C Program to Insert a Node at the End of a Linked List

C Program to Insert a Node at the End of a Linked List

Inserting a node at the end of a linked list is a common operation that allows adding elements in sequential order. Unlike inserting at the beginning, this operation requires traversal to the last node before adding the new node. Understanding how to append nodes efficiently is crucial for building dynamic lists and other data structures like queues.

Understanding the Problem

To insert a node at the end, the program must first find the last node in the list. Then, it links the new node to the last node’s next pointer. Proper pointer handling is critical to avoid losing nodes or creating broken links. Dynamic memory allocation is also needed to create the new node without overwriting existing data.

Program: Insert at the End

This program demonstrates how to dynamically create a new node and append it to the end of a singly linked list. It also handles the case where the list is initially empty.

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *head = NULL;

void insertAtEnd(int value) {

    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;

    if (!head) {
        head = newNode;  // If list is empty, new node becomes head
    } else {

        Node *temp = head;

        while (temp->next) {
            temp = temp->next; // Traverse to last node
        }

        temp->next = newNode; // Link new node to the last node
    }

    printf("%d inserted at the end of the list\n", value);

}

void display() {

    if (!head) {
        printf("Linked List is empty\n");
        return;
    }

    Node *temp = head;

    printf("Linked List: ");

    while (temp) {
        printf("%d ", temp->data);
        temp = temp->next;
    }

    printf("\n");

}

int main() {

    insertAtEnd(10);
    insertAtEnd(20);
    insertAtEnd(30);
    display();

    return 0;

}

In this program, the insertAtEnd function creates a new node and links it to the last node in the list. If the list is empty, the new node becomes the head. The display function traverses the list from head to NULL, printing all nodes sequentially.

FAQs

Q1: What is the time complexity of insertion at the end?
O(n), because it requires traversing the list to find the last node.

Q2: Can multiple nodes be inserted consecutively at the end?
Yes, each new node is linked to the last node, maintaining sequential order.

Q3: How is memory allocated for the new node?
Memory is dynamically allocated using malloc, and must be freed when the node is removed.

Q4: Can this method handle an initially empty list?
Yes, if the list is empty, the new node automatically becomes the head.

Conclusion

Inserting a node at the end of a linked list is an essential operation for maintaining sequential order in dynamic data structures. Careful pointer management and proper memory allocation ensure that the list remains intact while allowing efficient addition of new elements.

References & Additional Resources

The following books and online resources provide both theoretical foundations and practical examples to help you understand linked list operations and dynamic memory allocation in C.

  1. Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd Edition). Prentice Hall – Classic reference on C fundamentals, including pointers and memory, critical for linked list operations.
  2. Thareja, R. (2011). Data Structures Using C. Oxford University Press – Detailed coverage of linked lists and dynamic memory allocation techniques.
  3. GeeksforGeeks: Linked List Insertions – Step-by-step examples of insertion operations in linked lists.
  4. Programiz: Singly Linked List Basics – Beginner-friendly guide covering fundamental concepts and operations.
  5. Tutorialspoint: What is Dynamic Memory Allocation in C? – Explanation of malloc(), calloc(), realloc(), and free() functions for memory management in linked lists.
  6. C Standard Library Documentation – Memory Management – Official reference for memory allocation and deallocation functions in C.
Scroll to Top