I've created doubly linked list and I managed to print it going forward from head to tail, however I`m having trouble in doing it backwards. I get segmentation fault in line "current = current->prev", I don't understand why.
current = head;
while (current) {
printf("%p\t%s\t%d\n", current, current->name, current->age);
current = current->next;
}
current = current->prev;
while (current) {
printf("%p\t%s\t%d\n", current, current->name, current->age);
current = current->prev;
}
I have found how to fix this problem:
current = head;
while (current) {
printf("%p\t%s\t%d\n", current, current->name, current->age);
current = current->next;
}
current = head;
while (current->next) current = current->next;
while (current) {
printf("%p\t%s\t%d\n", current, current->name, current->age);
current = current->prev;
}
However, I still don't understand why my method didn't work. I would thankful if somebody could explain this.
for(current = head; current; current = current->next) {...}loops. It saves you more than two lines. /style - wildplasser