Hi!
Napisalem sobie prosta implementacje listy dwukierunkowej ale niestety w funkcji wypisujacej jej wszystkie elementy nastepuje zwis. Gdu debugowalem ten program to niby wszystko bylo ok, przechodzilo przez petle while, powinno wypisywac wartosci key ale nie wypisuje, i przy wyjsci z funkcji nastepuje zwis !! (i nic na wyjsice nie wypisuje) pomaga tylko ctrl+z. Tu jest kod wszystkiego:
#include "stdio.h"
typedef struct Element
{
int key;
Element *prev, *next;
};
class LList
{
public:
LList();
~LList();
Element *Search(int k);
int Insert(Element *x);
void Delete(Element *x);
void Print();
private:
Element *head;
};
LList::LList()
{
head = NULL;
}
LList::~LList()
{
Element *k = head;
while(k->next != NULL)
{
k = head->next;
delete k->prev;
}
delete k;
}
Element* LList::Search(int k)
{
Element *tmp = head;
while((k != tmp->key) && (tmp != NULL))
tmp = tmp->next;
return tmp;
}
int LList::Insert(Element *x) //at the beggining of the Linked-List
{
x->next = head;
if(head != NULL)
head->prev = x;
head = x;
x->prev = NULL;
printf("%d\t", x->key);
if(x->prev != NULL)
printf("%d\t", (x->prev)->key);
if(x->next != NULL)
printf("%d\n", (x->next)->key);
return 1; //exit normally
}
void LList::Delete(Element *x)
{
if(head != NULL)
{
(x->prev)->next = x->next;
(x->next)->prev = x->prev;
delete x;
}
}
<b>void LList::Print()
{
Element *x = head;
while(x != NULL)
{
printf("%d\t", x->key);
x = x->next;
}
}</b>
int main(int argc, char **argv)
{
LList linked_list;
int i = 1;
/*while(i != 0)
{
printf("Type new element value:\t");
scanf("%d", &i);
if(i == 0)
break;
Element *x = new Element;
x->key = i;
linked_list.Insert(x);
}*/
linked_list.Print();
for(i = 0; i < 5; i++)
{
Element *x = new Element;
x->key = i;
linked_list.Insert(x);
}
printf("Elementy dodane\n");
linked_list.Print();
return 0;
}
Ferenlna funkcje pogrubilem.
Of course to jest pod linuxem, kompilator g++.</b>