data structures - Freezing when added to linked list (C) -
my code compiles correctly, when execute, insertlast called twice , program freezes. can't see why work twice freeze up.
the code send nodes linked list:
int main () { linkedlist* canqueue=createlist(); for(ii = 0; ii < 10; ii++) { tincan* tempcan = (tincan*) malloc(sizeof(tincan)); insertlast(canqueue, tempcan); } return 0; }
and linked list methods used:
linkedlist* createlist() /*creates empty linked list*/ { linkedlist* mylist; mylist = (linkedlist*)malloc(sizeof(linkedlist)); mylist->head = null; return mylist; } void insertlast(linkedlist* list, tincan *newdata) { int ii = 1; linkedlistnode* newnode = (linkedlistnode*)malloc(sizeof(linkedlistnode)); newnode->data = newdata; newnode->next = null; if(list->head == null) { list->head = newnode; newnode->next=null; } else { linkedlistnode* current = list->head; while (current->next != null) { current = current->next; } current->next = newnode; ii++; } }
it kind of looks setting first node it's own neighbour. note working pointers , not copy underlying object.
list->head = newnode; newnode->next=null; current = list->head; current->next = newnode;
at start have head newnode current head (current = newnode) current.next = newnode (newnode.next = newnode). because in while loop loop through 1 node forever until exit program.
Comments
Post a Comment