Introduction
A linked list is a dynamic data structure where each element, called a node, contains a data part and a pointer to the next node. To insert a node at the end of a linked list, you need to traverse the list to the last node and then update the last node’s pointer to point to the new node.
Example:
- Input: A linked list with nodes containing [10, 20, 30], and a new node with data40.
- Output: The linked list after insertion will contain [10, 20, 30, 40].
Problem Statement
Create a C program that:
- Creates a singly linked list.
- Inserts a node at the end of the linked list.
- Displays the linked list after the insertion.
Solution Steps
- Include the Standard Libraries: Use #include <stdio.h>and#include <stdlib.h>for standard input-output functions and dynamic memory allocation.
- Define the Node Structure: Create a structure for the node containing an integer data part and a pointer to the next node.
- Implement the Function to Insert a Node at the End: Write a function that traverses the linked list to the last node and inserts a new node at the end.
- Implement the Function to Display the Linked List: Write a function to traverse and display the elements of the linked list.
- Create a Main Function: In the main()function, create the linked list, insert nodes at the end, and display the list.
C Program to Insert a Node at the End of a Linked List
#include <stdio.h>
#include <stdlib.h>
// Step 2: Define the Node Structure
struct Node {
    int data;
    struct Node* next;
};
// Function to insert a node at the end of the linked list
void insertAtEnd(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *head_ref;
    new_node->data = new_data;
    new_node->next = NULL;
    // If the linked list is empty, make the new node the head
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }
    // Otherwise, traverse to the last node
    while (last->next != NULL) {
        last = last->next;
    }
    // Change the next of the last node to point to the new node
    last->next = new_node;
}
// Function to display the linked list
void displayList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}
int main() {
    struct Node* head = NULL;
    // Insert elements at the end
    insertAtEnd(&head, 10);
    insertAtEnd(&head, 20);
    insertAtEnd(&head, 30);
    insertAtEnd(&head, 40);
    // Display the linked list
    printf("Linked List: ");
    displayList(head);
    return 0;  // Return 0 to indicate successful execution
}
Explanation
Step 2: Define the Node Structure
- The Nodestructure contains two members: an integerdatato store the node’s data, and a pointernextto point to the next node in the list.
Function to Insert a Node at the End
- The insertAtEndfunction takes a double pointer to the head of the linked list (head_ref) and the data to be inserted (new_data).
- A new node is allocated dynamically using malloc().
- If the linked list is empty, the new node is made the head of the list.
- Otherwise, the function traverses the list to the last node and updates the last node’s nextpointer to point to the new node.
Function to Display the Linked List
- The displayListfunction traverses the linked list from the head and prints each node’s data, followed by an arrow (->), until it reaches the end (NULL).
Main Function
- The mainfunction creates an empty linked list and inserts several nodes at the end using theinsertAtEndfunction.
- After the nodes are inserted, the displayListfunction is called to display the linked list.
Output Example
Example Output:
Linked List: 10 -> 20 -> 30 -> 40 -> NULL
Conclusion
This C program demonstrates how to insert a node at the end of a linked list. It covers basic concepts such as dynamic memory allocation, pointer manipulation, and linked list traversal, making it a useful example for beginners learning data structures in C programming.