C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Insert Node at a Specific Position

C Program: Insert Node at a Specific Position

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for a node

struct Node {

    int data;

    struct Node *next;

};

 

// Function to create a linked list

struct Node* createList(int n) {

    struct Node *head = NULL, *temp, *newNode;

    int data, i;

 

    for (i = 1; i <= n; i++) {

        newNode = (struct Node*)malloc(sizeof(struct Node));

        if (newNode == NULL) {

            printf("Memory allocation failed!\n");

            exit(0);

        }

 

        printf("Enter data for node %d: ", i);

        scanf("%d", &data);

        newNode->data = data;

        newNode->next = NULL;

 

        if (head == NULL) {

            head = newNode;

            temp = head;

        } else {

            temp->next = newNode;

            temp = temp->next;

        }

    }

 

    return head;

}

 

// Function to insert node at specific position

struct Node* insertAtPosition(struct Node *head, int data, int pos) {

    struct Node *newNode, *temp = head;

    int i;

 

    newNode = (struct Node*)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("Memory allocation failed!\n");

        exit(0);

    }

 

    newNode->data = data;

 

    // Insert at the beginning

    if (pos == 1) {

        newNode->next = head;

        head = newNode;

        return head;

    }

 

    // Traverse to (pos - 1)th node

    for (i = 1; i < pos - 1 && temp != NULL; i++) {

        temp = temp->next;

    }

 

    if (temp == NULL) {

        printf("Position out of range!\n");

        free(newNode);

        return head;

    }

 

    // Insert new node

    newNode->next = temp->next;

    temp->next = newNode;

 

    return head;

}

 

// Function to display the linked list

void displayList(struct Node *head) {

    struct Node *temp = head;

 

    if (head == NULL) {

        printf("\nThe linked list is empty.\n");

        return;

    }

 

    printf("\nLinked List Elements: ");

    while (temp != NULL) {

        printf("%d -> ", temp->data);

        temp = temp->next;

    }

    printf("NULL\n");

}

 

int main() {

    struct Node *head = NULL;

    int n, data, pos;

 

    printf("Enter the number of nodes: ");

    scanf("%d", &n);

 

    head = createList(n);

 

    printf("\nEnter data to insert: ");

    scanf("%d", &data);

 

    printf("Enter position to insert (1-based index): ");

    scanf("%d", &pos);

 

    head = insertAtPosition(head, data, pos);

 

    displayList(head);

 

    return 0;

}

Output

 
OUTPUT :

Enter the number of nodes: 3
Enter data for node 1: 10
Enter data for node 2: 20
Enter data for node 3: 30

Enter data to insert: 25
Enter position to insert (1-based index): 3

Linked List Elements: 10 -> 20 -> 25 -> 30 -> NULL

Explanation

  • createList() → Creates an initial linked list of n nodes.
  • insertAtPosition() → Inserts a new node at a given position:
    • If pos == 1, insert at beginning.
    • Otherwise, traverse to (pos-1)th node and insert there.
  • displayList() → Prints the list.