0
0
CHow-ToBeginner · 3 min read

How to Insert a Node in Linked List in C: Syntax and Example

To insert a node in a linked list in C, create a new struct node, set its data and next pointer, then adjust the previous node's next to point to this new node. This can be done at the beginning, middle, or end by changing pointers accordingly.
📐

Syntax

Here is the basic syntax to insert a new node in a linked list:

  • Create a new node using malloc.
  • Set the new node's data field.
  • Set the new node's next pointer to the next node in the list.
  • Update the previous node's next pointer to point to the new node.
c
struct node {
    int data;
    struct node *next;
};

// Insert new_node after prev_node
void insertAfter(struct node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("Previous node cannot be NULL\n");
        return;
    }
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}
💻

Example

This example shows how to insert nodes at the beginning and after a given node in a linked list, then prints the list.

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

struct node {
    int data;
    struct node *next;
};

// Insert at the beginning
void push(struct node** head_ref, int new_data) {
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

// Insert after a given node
void insertAfter(struct node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("Previous node cannot be NULL\n");
        return;
    }
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}

// Print the linked list
void printList(struct node *node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct node* head = NULL;

    push(&head, 3); // List: 3
    push(&head, 1); // List: 1 -> 3
    insertAfter(head, 2); // List: 1 -> 2 -> 3

    printList(head);
    return 0;
}
Output
1 -> 2 -> 3 -> NULL
⚠️

Common Pitfalls

Common mistakes when inserting nodes include:

  • Not allocating memory with malloc, causing crashes.
  • Forgetting to update the next pointers correctly, breaking the list.
  • Inserting after a NULL node without checking.
  • Not updating the head pointer when inserting at the beginning.
c
/* Wrong: Not updating next pointer properly */
void wrongInsert(struct node* prev_node, int new_data) {
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data = new_data;
    // Forgot to set new_node->next
    prev_node->next = new_node; // This breaks the list
}

/* Correct: Set new_node->next before linking */
void correctInsert(struct node* prev_node, int new_data) {
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}
📊

Quick Reference

Tips for inserting nodes in linked lists:

  • Always allocate memory with malloc and check for NULL.
  • Update next pointers carefully to maintain list integrity.
  • When inserting at the head, update the head pointer.
  • Check for NULL pointers before dereferencing.

Key Takeaways

Create a new node with malloc and set its data before linking it.
Always update the next pointers correctly to avoid breaking the list.
When inserting at the beginning, update the head pointer to the new node.
Check for NULL pointers to prevent runtime errors.
Use helper functions like push and insertAfter for cleaner code.