C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Circular linked list - Insertion Operations

C Program: Insertion in Circular Linked List

Circular Linked List

A Circular Linked List is a variation of the singly linked list in which:

  • The last node points back to the first node, creating a circular connection.
  • There is no NULL pointer at the end.

NOTE: This means if you traverse the list continuously, you’ll keep looping through the nodes endlessly.

 

Structure Definition

struct Node {

    int data;

    struct Node *next;

};

 

  • data stores the value of the node.
  • next stores the address of the next node.
  • In the last node, next points back to head (circular link).

 

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure definition

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));

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

 

// Function to create a circular linked list

struct Node* createCircularList(int n) {

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

    int data, i;

 

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

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

        scanf("%d", &data);

        newNode = createNode(data);

 

        if (head == NULL) {

            head = newNode;

            temp = head;

        } else {

            temp->next = newNode;

            temp = newNode;

        }

    }

    temp->next = head; // Make it circular

    return head;

}

 

// Function to display circular linked list

void displayCircularList(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

        printf("\nList is empty.\n");

        return;

    }

    printf("\nCircular Linked List: ");

    do {

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

        temp = temp->next;

    } while (temp != head);

    printf("(back to head)\n");

}

 

// Insert at beginning

struct Node* insertAtBeginning(struct Node *head, int data) {

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

 

    if (head == NULL) {

        newNode->next = newNode;

        head = newNode;

        return head;

    }

 

    // Move to last node

    while (temp->next != head)

        temp = temp->next;

 

    temp->next = newNode;

    newNode->next = head;

    head = newNode;

 

    return head;

}

 

// Insert at end

struct Node* insertAtEnd(struct Node *head, int data) {

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

 

    if (head == NULL) {

        newNode->next = newNode;

        head = newNode;

        return head;

    }

 

    while (temp->next != head)

        temp = temp->next;

 

    temp->next = newNode;

    newNode->next = head;

 

    return head;

}

 

// Insert at specific position

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

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

    int i;

 

    if (pos == 1)

        return insertAtBeginning(head, data);

 

    for (i = 1; i < pos - 1 && temp->next != head; i++)

        temp = temp->next;

 

    newNode->next = temp->next;

    temp->next = newNode;

 

    return head;

}

 

int main() {

    struct Node *head = NULL;

    int n, data, pos, choice;

 

    printf("Enter number of nodes: ");

    scanf("%d", &n);

    head = createCircularList(n);

    displayCircularList(head);

 

    do {

        printf("\nMenu:\n");

        printf("1. Insert at Beginning\n");

        printf("2. Insert at End\n");

        printf("3. Insert at Position\n");

        printf("4. Display List\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

        case 1:

            printf("Enter data to insert: ");

            scanf("%d", &data);

            head = insertAtBeginning(head, data);

            break;

 

        case 2:

            printf("Enter data to insert: ");

            scanf("%d", &data);

            head = insertAtEnd(head, data);

            break;

 

        case 3:

            printf("Enter data and position: ");

            scanf("%d %d", &data, &pos);

            head = insertAtPosition(head, data, pos);

            break;

 

        case 4:

            displayCircularList(head);

            break;

 

        case 5:

            printf("Exiting...\n");

            break;

 

        default:

            printf("Invalid choice!\n");

        }

    } while (choice != 5);

 

    return 0;

}

Output

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

Circular Linked List: 10 -> 20 -> 30 -> (back to head)

Menu:
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Display List
5. Exit
Enter your choice: 1
Enter data to insert: 5

Circular Linked List: 5 -> 10 -> 20 -> 30 -> (back to head)

Explanation

1. Creating a Circular Linked List

struct Node* createCircularList(int n)

Steps:

  1. Input the number of nodes (n).
  2. Create each node using malloc() and assign data.
  3. Link nodes sequentially.
  4. Finally, make the last node point to the head:

5.  temp->next = head;

This creates the circular structure.

Example:

If you input 3 nodes (10, 20, 30), links look like this:

10 → 20 → 30    

 ↑_________|

2. Displaying the Circular Linked List

void displayCircularList(struct Node *head)

Steps:

  1. Start from the head node.
  2. Use a ..while loop (because we must print the head once before checking condition).
  3. Continue traversing and printing until the next node is again head.

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

3. Insertion at the Beginning

struct Node* insertAtBeginning(struct Node *head, int data)

Steps:

  1. Create a new node using malloc().
  2. If the list is empty, make it circular itself:

3.  newNode->next = newNode;4.  head = newNode;

  1. Otherwise:
    • Traverse to the last node.
    • Point the new node to head.
    • Make the last node’s next point to the new node.
    • Update head to the new node.

Diagram:

Before insertion:

10 → 20 → 30 → back to 10

After inserting 5 at beginning:

5 → 10 → 20 → 30 → back to 5

4. Insertion at the End

struct Node* insertAtEnd(struct Node *head, int data)

Steps:

  1. Create a new node.
  2. If list is empty, make it point to itself.
  3. Otherwise:
    • Traverse to the last node.
    • Set new node’s next = head.
    • Set last node’s next = newNode.

Diagram:

Before insertion:

10 → 20 → 30 → back to 10

After inserting 40:

10 → 20 → 30 → 40 → back to 10

5. Insertion at a Specific Position

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

Steps:

  1. If pos == 1, use the insertAtBeginning()
  2. Otherwise:
    • Traverse (pos - 1) nodes using a loop.
    • Insert the new node by adjusting pointers:

o   newNode->next = temp->next;o   temp->next = newNode;

Example:

Before insertion:

10 → 20 → 30 → back to 10

Insert 15 at position 2:

10 → 15 → 20 → 30 → back to 10