Mam strukturę:
typedef struct elem {
struct elem *next;
void* dane;
} elem_t;
Zacząłem piasć program w Code::Blocks (kompilator GNU GCC), który tworzy listę pojedynczo łączoną i wykonuje na niej podstawowe operacje.
Nie działa poprawnie funkcja wypisz, tzn. wypisuje stałą dużą liczbę.
void wypisz_liste(elem_t* e){
if(!e) printf("Lista jest pusta.\n");
elem_t *this=NULL;
this=e;
while (this)
{
//this->dane = (int*) this->dane;
printf ("%d\t", *((int*)(this->dane)));
this = this->next;
}
}
W funkcji tworzącej węzeł wypisuję wpisane dane i to działa poprawnie, wypisuje kolejne liczby naturalne:
void dodaj_element( elem_t** e, void* dane)
{
elem_t *this=NULL;
if (*e)
{
this=*e;
while(this->next)
{
this=this->next;
}
this->next = (elem_t*)malloc(sizeof(elem_t));
this->next->dane=dane;
this->next->next=NULL;
printf("%d\t", *((int*)(this->next->dane)));
}
else
{
(*e)=(elem_t*)malloc(sizeof(elem_t));
(*e)->dane=dane;
(*e)->next= NULL;
printf("%d\n", *((int*)((*e)->dane)));
printf("Utworzylem pierwszy wezel\n");
}
}
Efekt można zaobserwować kompilując i uruchamiając kod źródłowy z załączników.