Insert Node at End Of Linked List In C

When inserting a node at the end of a linked list, we typically follow these steps:

  • Create a new node: Allocate memory and initialize it with the desired data.
  • Traverse to the last node: Start at the head and iterate through the list until the last node is found.
  • Link the new node: Make the next pointer of the last node point to the newly created node.

Step 01: Node Structure in Linked List

A linked list is a collection of nodes, each consisting of two components:

  • Data: The value stored in the node.
  • Pointer: A reference to the next node in the sequence.

The structure of a node in the linked list is described below.

 

Node Structure in Linked List

 

Step 02: Consider a Singly-linked List

Consider a singly linked list consisting of four nodes with the following data and addresses:

  • The first node contains the value 10 at address 350 
  • The second node contains the value 20 at address 380 
  • The third node contains the value 30 at address 700 
  • The fourth node contains the value 40 at address 900

Each node points to the next one in the list, and the last node points to NULL, indicating the end of the list.

Example A Singly Linked List

Step 03: Create New Node for insertion (“ptr2”)

To insert a new node (suppose data=70 and address =1200) at the end of a singly linked list, first, create the new node and assign its data and address.

Create new Node in Linked list

 

 

Note: In this position, we need to take an additional pointer, “ptr2,” which will point to the new node.

Step 04: Create New Pointer (“ptr”)

We need another pointer (i.e. ptr) to find the last node. It starts traversing from the first node (called the head).

Create new pointer (”ptr”)

Step 05: Traverse the (“ptr”) to last node

“Traverse the ptr to the last node” means iterating through a linked list starting from the head node until you reach the last node, where the next pointer of that node is NULL. The following code will help to reach at the last node in the list.

// Assuming head is a pointer to the first node in the list
struct Node *ptr = head;while (ptr->next != NULL) {
ptr = ptr->next; // Move to the next node
}
// Now, ptr points to the last node

Traverse (”ptr”) to Last Node

Step 06: Insert Node through (ptr->next = ptr2)

As ptr->next already contains Null address. By using ptr->next = ptr2, The ptr->next is updated from Null to 1200 address

Mathematically,
ptr->next = Null;
ptr2 =1200;
ptr->next = ptr2,  So
ptr->next = 1200;

The following figure explains it all

Insert Node at the end of Linked List

Code Example in C: 

#include <stdio.h>
#include <stdlib.h>// Define the structure of a node
struct Node {
int data;
struct Node* next;
};

// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf(“Memory allocation failed\n”);
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end of the linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* ptr2 = createNode(data); // `ptr2` is used for the new node

if (head == NULL) {
// If the list is empty, the new node becomes the head
return ptr2;
}

struct Node* ptr = head; // `ptr` is used for traversal

// Traverse to the end of the list
while (ptr->next != NULL) {
ptr = ptr->next;
}

// Insert the new node at the end
ptr->next = ptr2;

return head;
}

// Function to display the linked list
void displayList(struct Node* head) {
struct Node* ptr = head; // Use `ptr` for traversal
while (ptr != NULL) {
printf(“%d -> “, ptr->data);
ptr = ptr->next;
}
printf(“NULL\n”);
}

// Main function
int main() {
// Manually creating the linked list
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
head->next->next->next = createNode(40);

printf(“Initial linked list: “);
displayList(head);

// Insert a new node with value 15 at the end
int newData = 70;

printf(“\nInserting %d at the end…\n”, newData);
head = insertAtEnd(head, newData);

printf(“Linked list after insertion: “);
displayList(head);

return 0;
}

 

Output:

Initial linked list: 10 -> 20 -> 30 -> 40 -> NULL

Inserting 15 at the end...
Linked list after insertion: 10 -> 20 -> 30 -> 40 -> 15 -> NULL