C insert node list at the end

I am having some problems with my insertion method for a linked list in C. It seems to add only at the top of the list. Any other insertion I make fails. And this CodeBlocks debugger is so hard to understand that I still don't understand it. It never gives me meaning, just an address in mind. Anyway, this is my function. Do you see the reason this fails?

/* function to add a new node at the end of the list */ int addNodeBottom(int val, node *head){ //create new node node *newNode = (node*)malloc(sizeof(node)); if(newNode == NULL){ fprintf(stderr, "Unable to allocate memory for new node\n"); exit(-1); } newNode->value = val; //check for first insertion if(head->next == NULL){ head->next = newNode; printf("added at beginning\n"); } else { //else loop through the list and find the last //node, insert next to it node *current = head; while(current->next != NULL) { if(current->next == NULL) { current->next = newNode; printf("added later\n"); } current = current->next; } } return 0; } 

Then basically only 929 is added.

  //testing addNodeBottom function addNodeBottom(929, head); addNodeBottom(98, head); addNodeBottom(122, head); addNodeBottom(11, head); addNodeBottom(1034, head); 
+8
source share
6 answers

This code will work. The answer from samplebias is almost correct, but you need a third change:

 int addNodeBottom(int val, node *head){ //create new node node *newNode = (node*)malloc(sizeof(node)); if(newNode == NULL){ fprintf(stderr, "Unable to allocate memory for new node\n"); exit(-1); } newNode->value = val; newNode->next = NULL; // Change 1 //check for first insertion if(head->next == NULL){ head->next = newNode; printf("added at beginning\n"); } else { //else loop through the list and find the last //node, insert next to it node *current = head; while (true) { // Change 2 if(current->next == NULL) { current->next = newNode; printf("added later\n"); break; // Change 3 } current = current->next; }; } return 0; } 

Edit 1: newNode->next should be set to NULL , so we do not insert invalid pointers at the end of the list.

Edit 2/3: the loop changes to an infinite loop that will slip out with break; when we find the last item. Notice how while(current->next != NULL) used to be rejected if(current->next == NULL) .

EDIT. As for the while loop, this is much better:

  node *current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; printf("added later\n"); 
+12
source

After malloc a node set node->next = NULL .

 int addNodeBottom(int val, node *head) { node *current = head; node *newNode = (node *) malloc(sizeof(node)); if (newNode == NULL) { printf("malloc failed\n"); exit(-1); } newNode->value = val; newNode->next = NULL; while (current->next) { current = current->next; } current->next = newNode; return 0; } 

I must point out that with this version, head is still used as a dummy, not used to store the value. This allows you to present an empty list with only a head node.

+2
source

I would like to mention the key before writing the code for your consideration.

// Key

temp = address of the new node allocated by the malloc function (member library od alloc.h in C)

prev = address of the last node of the existing list of links.

next = contains the address of the next node

 struct node { int data; struct node *next; } *head; void addnode_end(int a) { struct node *temp, *prev; temp = (struct node*) malloc(sizeof(node)); if (temp == NULL) { cout << "Not enough memory"; } else { node->data = a; node->next = NULL; prev = head; while (prev->next != NULL) { prev = prev->next; } prev->next = temp; } } 
0
source

A new node is always added after the last node of this Linked List. For example, if this Linked List is 5-> 10-> 15-> 20-> 25, and we add paragraph 30 at the end, then the Holy List will become 5-> 10-> 15-> 20-> 25-> 30. Since the linked list is usually represented by its chapter, we need to go through the list to the end, and then change the next node from the last to a new node.

 /* Given a reference (pointer to pointer) to the head of a list and an int, appends a new node at the end */ void append(struct node** head_ref, int new_data) { /* 1. allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); struct node *last = *head_ref; /* used in step 5*/ /* 2. put in the data */ new_node->data = new_data; /* 3. This new node is going to be the last node, so make next of it as NULL*/ new_node->next = NULL; /* 4. If the <a href="#">Linked List</a> is empty, then make the new node as head */ if (*head_ref == NULL) { *head_ref = new_node; return; } /* 5. Else traverse till the last node */ while (last->next != NULL) last = last->next; /* 6. Change the next of last node */ last->next = new_node; return; } 
0
source

This works great:

 struct node *addNode(node *head, int value) { node *newNode = (node *) malloc(sizeof(node)); newNode->value = value; newNode->next = NULL; if (head == NULL) { // Add at the beginning head = newNode; } else { node *current = head; while (current->next != NULL) { current = current->next; }; // Add at the end current->next = newNode; } return head; } 

Usage example:

 struct node *head = NULL; for (int currentIndex = 1; currentIndex < 10; currentIndex++) { head = addNode(head, currentIndex); } 
0
source

I know this is an old post, but for reference only. Here's how you can add validation without a special case to an empty list, although at the expense of more complex code.

 void Append(List * l, Node * n) { Node ** next = &list->Head; while (*next != NULL) next = &(*next)->Next; *next = n; n->Next = NULL; } 
0
source

All Articles