c++ - linkedlist, cannot link the node to head -


i lost direction in middle of somewhere, cannot figure out wrong code. functions below append function put node list.

void appendnode(struct s_list *list, unsigned int data) {     if (list == nullptr)         return;      struct s_node *temphead = list->head;     struct s_node *newnode = new s_node;     newnode->next = nullptr;      while (temphead != nullptr)         temphead = temphead->next;      temphead = newnode; } 

i'm calling function 100 times , won't link new node current list @ all. shouldn't hard find problem i'm bad. give me advice. thanks.

//*************************************************************************//

thanks reply still have same problem. allocate head node list pass function. now, changed pass head of list directly, not list anymore still have same issue...

void appendnode(struct s_node *head, unsigned int data) {     if (head == nullptr)         return;      struct s_node *temphead = head;     struct s_node *newnode = new s_node;     newnode->next = nullptr;      while (temphead != nullptr)         temphead = temphead->next;      temphead = newnode; } 

your temphead runs right off end of list; nothing in function changes list.

first deal case of empty list:

if(list->head == null) {   list->head = newnode;   return; } 

then advance caution:

while (temphead->next != nullptr)   temphead = temphead->next;  temphead->next = newnode; 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -