2015-11-07

Write a program to create a circular linked list and insert a new node at the beginning or at any position in the given list. How to insert a new node at the beginning of a circular linked list in C. How to insert a new node at any position in a circular linked list in C.



Required knowledge
Basic C programming, Dynamic memory allocation, Circular linked list

Algorithm to insert a new node at the beginning of a Circular linked list
For inserting new node at the beginning of a circular linked list. We need to connect new node with the first node and re-connect the last node with new node instead of head node.

Algorithm to insert new node at the any position in Circular linked list

Steps to insert new node in a Circular linked list
Suppose we want to insert a new node in the circular linked list at 3 position i.e. just after 2nd position. The list initially contains 3 nodes. We will follow below steps to insert node at 2nd position in the list.

Create a newNode and assign some data to its data field.



Traverse to N-1 position in the list, in our case since we want to insert node at 3rd position therefore we would traverse to 3-1 = 2nd position in the list. Say current pointer points to N-1th node.



Link the next pointer field of newNode with the node pointed by the next pointer field of current(N-1) node. Which means newNode.next = current.next.

Connect the next pointer field of current node with the newly created node which means now next pointer field of current node will point to newNode. And you are done now.

Program

Output
X



_

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 1
Enter the total number of nodes in list: 2
Enter data of 1 node: 20
Enter data of 2 node: 40

CIRCULAR LINKED LIST CREATED SUCCESSFULLY

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 2
DATA IN THE LIST:
Data 1 = 20
Data 2 = 40

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 3
Enter data to be inserted at beginning: 10
NODE INSERTED SUCCESSFULLY

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 2
DATA IN THE LIST:
Data 1 = 10
Data 2 = 20
Data 3 = 40

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 4
Enter node position: 3
Enter data you want to insert at 3 position: 30
NODE INSERTED SUCCESSFULLY.

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 2
DATA IN THE LIST:
Data 1 = 10
Data 2 = 20
Data 3 = 30
Data 4 = 40

============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 0

Happy coding ;)

Show more