Program for all operations on Circular Linked List in C
Last Updated :
27 Apr, 2023
In a Circular linked list, every element has a link to its next element in the sequence, and the last element has a link to the first element. A circular linked list is similar to the singly linked list except that the last node points to the first node. Below is the image to illustrate the same:
1. Insertion at the beginning:
Insert a new node as the first node. The next pointer of last will point to this node and this new node will point to the previous first node.
Below is the implementation of the above operation:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void insertAtFront( int data)
{
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
}
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
insertAtFront(10);
insertAtFront(20);
insertAtFront(30);
viewList();
return 0;
}
|
Output
Data = 30
Data = 20
Data = 10
The time complexity of the insertAtFront function is O(1) as it performs a constant amount of work to insert a node at the front of the linked list.
The time complexity of the viewList function is O(n) as it has to traverse the entire linked list to print its elements.
2. Insertion at the end:
Inserting a new node as the last node. The next pointer of last will point to this node and this new node will point to the first node.
Below is the implementation of the above operation:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void addatlast( int data)
{
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast(10);
addatlast(20);
addatlast(30);
viewList();
return 0;
}
|
Output
Data = 10
Data = 20
Data = 30
3. Insertion after a specific element:
Below is the program to insert a node after a specified node in the linked list:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void addatlast()
{
int data;
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
printf ( "\nEnter data to be inserted : \n" );
scanf ( "%d" , &data);
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void insertafter()
{
int data, value;
struct node *temp, *n;
printf ( "\nEnter number after which"
" you want to enter number: \n" );
scanf ( "%d" , &value);
temp = last->next;
do {
if (temp->info == value) {
n = ( struct node*) malloc ( sizeof ( struct node));
printf ( "\nEnter data to be"
" inserted : \n" );
scanf ( "%d" , &data);
n->info = data;
n->next = temp->next;
temp->next = n;
if (temp == last)
last = n;
break ;
}
else
temp = temp->next;
} while (temp != last->next);
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast();
addatlast();
addatlast();
insertafter();
viewList();
return 0;
}
|
Output:
4. Delete the first element:
Deleting the first node of the linked list. For this, the next pointer of the last will point to the second node of the linked list.
Below is the implementation of the above operation:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void addatlast( int data)
{
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void deletefirst()
{
struct node* temp;
if (last == NULL)
printf ( "\nList is empty.\n" );
else {
temp = last->next;
last->next = temp->next;
free (temp);
}
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast(10);
addatlast(20);
addatlast(30);
printf ( "Before deletion:\n" );
viewList();
deletefirst();
printf ( "\n\nAfter deletion:\n" );
viewList();
return 0;
}
|
Output
Before deletion:
Data = 10
Data = 20
Data = 30
After deletion:
Data = 20
Data = 30
5. Delete the last element:
Deleting the last node of the linked list. For this, the second last node will point to the first node of the list.
Below is the implementation of the above operation:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void addatlast( int data)
{
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void deletelast()
{
struct node* temp;
if (last == NULL)
printf ( "\nList is empty.\n" );
temp = last->next;
while (temp->next != last)
temp = temp->next;
temp->next = last->next;
last = temp;
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast(10);
addatlast(20);
addatlast(30);
printf ( "Before Deletion:\n" );
viewList();
deletelast();
printf ( "\n\nAfter Deletion:\n" );
viewList();
return 0;
}
|
Output
Before Deletion:
Data = 10
Data = 20
Data = 30
After Deletion:
Data = 10
Data = 20
6. Delete at a given position:
Delete an element from a specified position in the linked list
Below is the implementation of the above operation:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
};
struct node* last = NULL;
void addatlast()
{
int data;
struct node* temp;
temp = ( struct node*) malloc ( sizeof ( struct node));
printf ( "\nEnter data to be inserted : \n" );
scanf ( "%d" , &data);
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void deleteAtIndex()
{
int pos, i = 1;
struct node *temp, *position;
temp = last->next;
if (last == NULL)
printf ( "\nList is empty.\n" );
else {
printf ( "\nEnter index : " );
scanf ( "%d" , &pos);
while (i <= pos - 1) {
temp = temp->next;
i++;
}
position = temp->next;
temp->next = position->next;
free (position);
}
}
void viewList()
{
if (last == NULL)
printf ( "\nList is empty\n" );
else {
struct node* temp;
temp = last->next;
do {
printf ( "\nData = %d" , temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast();
addatlast();
addatlast();
deleteAtIndex();
viewList();
return 0;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...