Insertion in circular linked list: Beginning ,End and at any given position
Updated on September 20, 2025
- Algorithm
- C program
- Explanation of Code
- 1. Structure Definition
- 2. Node Creation
- 3. Insert Node at Beginning
- 4. View / Traverse List
- 5. Menu Function
- 6. Main Function
- Time & Space Complexity
- Algorithm
- C Program
- Explanation of Code
- 1. Structure Definition
- 2. Node Creation
- 3. Insert Node at End
- 4. View / Traverse List
- 5. Menu Function
- 6. Main Function
- Time & Space Complexity
- Algorithm
- C Program
- Explanation of Code
- 1. Structure Definition
- 2. Node Creation
- 3. Insert Node at End
- 4. Insert Node at a Given Location
- 5. View / Traverse List
- 6. Menu Function
- 7. Main Function
- Time & Space Complexity
- Insertion at Beginning: A new node is created, its
nextpointer is set to the current head, and the last node’snextpointer is updated to point to this new node. Finally, the head pointer is moved to the new node. - Insertion at End: A new node is created, its
nextpointer is set to the head, and the current last node’snextpointer is updated to this new node, making it the new last node in the list. - Insertion at a Given Position: Insertion at any position in a circular linked list involves traversing to the node before the desired position and linking a new node between the previous and next nodes. This ensures the circular structure of the list is maintained.
In both cases, the circular property is maintained because the last node always points back to the head.
Insert a node in Beginning of circular linked list
Algorithm
Step 1: Start.
Step 2: Create a new node newNode and assign data to it.
Step 3: If the list is empty (head == NULL):
a. Set head = newNode.
b. Point newNode->next = head to make it circular.
Step 4: Else (list is not empty):
a. Traverse to the last node of the list.
b. Set newNode->next = head.
c. Update the last node’s next to point to newNode.
d. Move head pointer to newNode.
Step 5: Stop
C program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return (newNode);
}
void insertNodeAtBegin(){
struct node *newNode, *temp, *ptr1, *ptr2;
newNode=createNode();
printf("Enter a data to insert at begining:");
scanf("%d",&newNode->data);
newNode->next=NULL;
if(head==NULL){
head=newNode;
head->next=head;
}
else{
ptr1=head;
ptr2=head;
while(ptr1->next!=head){
ptr1=ptr1->next;
}
ptr1->next=newNode;
head=newNode;
head->next=ptr2;
}
}
void viewList(){
struct node* temp=head;
if(temp==NULL){
printf("list is empty");
}
else{
printf("Data in circular linked list are:\n");
while(temp->next!=head)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d \t",temp->data);
}
}
int menu(){
int choice;
printf("\n 1. Insert at begining");
printf("\n 2. Travesre/View List");
printf("\n 3. exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main(){
printf("Insert a node in Beginning of circular linked list");
while(1){
switch(menu()){
case 1:
insertNodeAtBegin();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("invalid choice");
}
getch();
}
}
Output:
Insert a node in Beginning of circular linked list
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at begining:4
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at begining:7
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at begining:9
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at begining:5
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 2
Data in circular linked list are:
5 9 7 4
1. Insert at begining
2. Travesre/View List
3. exit
Please enter your choice: 3
Explanation of Code
1. Structure Definition
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
- Each node contains:
data: integer valuenext: pointer to the next node
headpoints to the first node of the circular linked list.
2. Node Creation
struct node* createNode() {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return newNode;
}
- Dynamically allocates memory for a new node.
- Returns pointer to the newly created node.
3. Insert Node at Beginning
void insertNodeAtBegin() {
struct node *newNode, *temp, *ptr1, *ptr2;
newNode = createNode();
printf("Enter a data to insert at beginning: ");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) { // if list is empty
head = newNode;
head->next = head; // points to itself
} else {
ptr1 = head;
ptr2 = head;
while (ptr1->next != head) { // move to last node
ptr1 = ptr1->next;
}
ptr1->next = newNode; // last node points to new node
head = newNode; // update head
head->next = ptr2; // new node points to previous head
}
}
Logic:
- If list is empty → new node points to itself and becomes head.
- Otherwise → traverse to last node, link it to new node, update
head, and new node points to old head.
4. View / Traverse List
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty");
} else {
printf("Data in circular linked list are:\n");
while (temp->next != head) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data); // print last node
}
}
- Traverses from
headand prints all nodes until it returns tohead.
5. Menu Function
int menu() {
int choice;
printf("\n 1. Insert at beginning");
printf("\n 2. Traverse/View List");
printf("\n 3. Exit");
printf("\n Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
- Provides user options to insert, view, or exit.
6. Main Function
void main() {
printf("Insert a node at Beginning of circular linked list");
while (1) {
switch(menu()) {
case 1:
insertNodeAtBegin();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("Invalid choice");
}
getch();
}
}
- Runs continuously until user selects exit.
- Calls functions based on menu choice.
Time & Space Complexity
| Operation | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Insert at Beginning (empty list) | O(1) | O(1) | O(1) | O(1) |
| Insert at Beginning (non-empty) | O(n) | O(n) | O(n) | O(1) |
| Traverse / View List | O(n) | O(n) | O(n) | O(1) |
Insert a node at end of Circular Linked list
Algorithm
- Start.
- Create a new node and input its data.
- If
head == NULL→ make new node point to itself and sethead = new node. - Else → traverse the list to reach the last node.
- Link the last node’s
nextto new node. - Link new node’s
nexttohead. - End
C Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return (newNode);
}
void insertNodeAtEnd(){
struct node *temp,*ptr;
temp=createNode();
printf("Enter a data to insert at end:");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=temp;
head->next=head;
}
else{
ptr=head;
while(ptr->next!=head){
ptr=ptr->next;
}
ptr->next=temp;
temp->next=head;
}
}
void viewList(){
struct node* temp=head;
if(temp==NULL){
printf("list is empty");
}
else{
printf("Data in circular linked list are:\n");
while(temp->next!=head)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d \t",temp->data);
}
}
int menu(){
int choice;
printf("\n 1. Insert at end");
printf("\n 2. Travesre/View List");
printf("\n 3. exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main(){
printf("Insert a node at end of Circular Linked list");
while(1){
switch(menu()){
case 1:
insertNodeAtEnd();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("invalid choice");
}
getch();
}
}
Output:
Insert a node at end of Circular Linked list
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at end:4
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at end:6
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at end:3
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 1
Enter a data to insert at end:2
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 2
Data in circular linked list are:
4 6 3 2
1. Insert at end
2. Travesre/View List
3. exit
Please enter your choice: 3
Explanation of Code
1. Structure Definition
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
- Each node contains:
data: integer valuenext: pointer to the next node
headpoints to the first node of the circular linked list.
2. Node Creation
struct node* createNode() {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return newNode;
}
- Dynamically allocates memory for a new node.
- Returns pointer to the newly created node.
3. Insert Node at End
void insertNodeAtEnd() {
struct node *temp, *ptr;
temp = createNode();
printf("Enter a data to insert at end: ");
scanf("%d", &temp->data);
temp->next = NULL;
if (head == NULL) { // if list is empty
head = temp;
head->next = head; // first node points to itself
} else {
ptr = head;
while (ptr->next != head) { // traverse to last node
ptr = ptr->next;
}
ptr->next = temp; // link last node to new node
temp->next = head; // new node points to head
}
}
Logic:
- If the list is empty → the new node becomes head and points to itself.
- Otherwise → traverse to the last node, insert new node, and link it back to head.
4. View / Traverse List
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty");
} else {
printf("Data in circular linked list are:\n");
while (temp->next != head) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data); // print last node
}
}
- Traverses from
headand prints all nodes until it comes back tohead.
5. Menu Function
int menu() {
int choice;
printf("\n 1. Insert at end");
printf("\n 2. Traverse/View List");
printf("\n 3. Exit");
printf("\n Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
- Provides user options → insert at end, view, or exit.
6. Main Function
void main() {
printf("Insert a node at end of Circular Linked list");
while (1) {
switch(menu()) {
case 1:
insertNodeAtEnd();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("Invalid choice");
}
getch();
}
}
- Runs continuously until user selects exit.
- Calls functions based on menu choice.
Time & Space Complexity
| Operation | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Insert at End (empty list) | O(1) | O(1) | O(1) | O(1) |
| Insert at End (non-empty list) | O(n) | O(n) | O(n) | O(1) |
| Traverse / View List | O(n) | O(n) | O(n) | O(1) |
Insert a node at a given position in Circular Linked List
Algorithm
- Start.
- Create a new node and input its data.
- Insert at end:
- If list is empty → node points to itself, set
head. - Else → traverse to last node, link new node, and link back to
head.
- If list is empty → node points to itself, set
- Insert at given position:
- If position = 1 → same as inserting at beginning.
- Else → traverse to
(position-1)node and insert new node. - Invalid position → show error.
- End.
C Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node* createNode() {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return newNode;
}
void insertNodeAtEnd() {
struct node *temp, *ptr;
temp = createNode();
printf("Enter a data to insert at end: ");
scanf("%d", &temp->data);
temp->next = NULL;
if (head == NULL) {
head = temp;
head->next = head;
} else {
ptr = head;
while (ptr->next != head) {
ptr = ptr->next;
}
ptr->next = temp;
temp->next = head;
}
}
void insertNodeAtLocation() {
int pos, i = 1;
struct node *temp, *ptr;
temp = createNode();
printf("Enter position to insert: ");
scanf("%d", &pos);
printf("Enter data to insert at position %d: ", pos);
scanf("%d", &temp->data);
temp->next = NULL;
if (pos == 1) {
if (head == NULL) {
head = temp;
head->next = head;
} else {
ptr = head;
while (ptr->next != head) {
ptr = ptr->next;
}
temp->next = head;
head = temp;
ptr->next = head;
}
} else {
ptr = head;
while (i < pos - 1 && ptr->next != head) {
ptr = ptr->next;
i++;
}
if (i == pos - 1) {
temp->next = ptr->next;
ptr->next = temp;
} else {
printf("Invalid position!\n");
free(temp);
}
}
}
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
} else {
printf("Data in circular linked list are:\n");
while (temp->next != head) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data);
}
}
int menu() {
int choice;
printf("\n--- MENU ---");
printf("\n1. Insert at end");
printf("\n2. Insert at a given location");
printf("\n3. Traverse/View List");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
return choice;
}
void main() {
printf("Insertion in Circular Linked List\n");
while (1) {
switch (menu()) {
case 1:
insertNodeAtEnd();
break;
case 2:
insertNodeAtLocation();
break;
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
getch();
}
}
Output:
Insertion in Circular Linked List
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 1
Enter a data to insert at end: 4
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 1
Enter a data to insert at end: 6
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 1
Enter a data to insert at end: 8
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 1
Enter a data to insert at end: 3
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 3
Data in circular linked list are:
4 6 8 3
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 2
Enter position to insert: 2
Enter data to insert at position 2: 8
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 3
Data in circular linked list are:
4 8 6 8 3
--- MENU ---
1. Insert at end
2. Insert at a given location
3. Traverse/View List
4. Exit
Enter your choice: 3
Explanation of Code
1. Structure Definition
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
- Each node contains:
data: integer valuenext: pointer to the next node
headpoints to the first node of the circular linked list.
2. Node Creation
struct node* createNode() {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return newNode;
}
- Dynamically allocates memory for a new node.
- Returns pointer to the newly created node.
3. Insert Node at End
void insertNodeAtEnd() {
struct node *temp, *ptr;
temp = createNode();
printf("Enter a data to insert at end: ");
scanf("%d", &temp->data);
temp->next = NULL;
if (head == NULL) { // if list is empty
head = temp;
head->next = head; // first node points to itself
} else {
ptr = head;
while (ptr->next != head) { // traverse to last node
ptr = ptr->next;
}
ptr->next = temp; // link last node to new node
temp->next = head; // new node points to head
}
}
Logic:
- If list is empty → new node becomes head and points to itself.
- Otherwise → traverse to last node, insert new node, and link it back to head.
4. Insert Node at a Given Location
void insertNodeAtLocation() {
int pos, i = 1;
struct node *temp, *ptr;
temp = createNode();
printf("Enter position to insert: ");
scanf("%d", &pos);
printf("Enter data to insert at position %d: ", pos);
scanf("%d", &temp->data);
temp->next = NULL;
if (pos == 1) { // Insert at beginning
if (head == NULL) {
head = temp;
head->next = head;
} else {
ptr = head;
while (ptr->next != head) {
ptr = ptr->next; // last node
}
temp->next = head;
head = temp; // update head
ptr->next = head; // last node points to new head
}
} else { // Insert at position > 1
ptr = head;
while (i < pos - 1 && ptr->next != head) {
ptr = ptr->next;
i++;
}
if (i == pos - 1) {
temp->next = ptr->next;
ptr->next = temp; // link node at position
} else {
printf("Invalid position!\n");
free(temp);
}
}
}
Logic:
- Position = 1: behaves like inserting at the beginning.
- Position > 1: traverse to
(position-1)node and insert new node. - Invalid position → prints error and frees memory.
5. View / Traverse List
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
} else {
printf("Data in circular linked list are:\n");
while (temp->next != head) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data); // print last node
}
}
- Traverses from
headand prints all nodes until returning tohead.
6. Menu Function
int menu() {
int choice;
printf("\n--- MENU ---");
printf("\n1. Insert at end");
printf("\n2. Insert at a given location");
printf("\n3. Traverse/View List");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
return choice;
}
- Provides user options → insert at end, insert at position, view, or exit.
7. Main Function
void main() {
printf("Insertion in Circular Linked List\n");
while (1) {
switch (menu()) {
case 1:
insertNodeAtEnd();
break;
case 2:
insertNodeAtLocation();
break;
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
getch();
}
}
- Runs continuously until user selects exit.
- Calls respective functions based on user choice.
Time & Space Complexity
| Operation | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Insert at End (empty list) | O(1) | O(1) | O(1) | O(1) |
| Insert at End (non-empty list) | O(n) | O(n) | O(n) | O(1) |
| Insert at Position = 1 | O(1) | O(n) | O(n) | O(1) |
| Insert at Position > 1 | O(n) | O(n) | O(n) | O(1) |
| Traverse / View List | O(n) | O(n) | O(n) | O(1) |