The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Circular Linked List

In circular linked list, the node node contains a ponter to the first node of the list. In circular linked list, we can begin at any node and traverse the list in any direction ie) forward or backward direction of traverse is possible, until we reach the same node. Thus circular linked list can have circular singly linked list as well as a circular doubly linked list.

Structure - Circular Linked List

Structure - Circular Linked List
struct node
{
int data;
struct node *next;
};

C program - Circular Linked List

Here is the program to demonstrate, how to create and use circular linked list.

circular-linked-list.c
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_circularlinkedlist(struct node *);
struct node *display(struct node *);
int main()
{
int choice;
do {
printf("\n1. Create a list");
printf("\n2. Display the list");
printf("\n3. Exit the list");
printf("\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
start = create_circularlinkedlist(start);
printf("\n Linked List Created Successfully");
break;
case 2:
start = display(start);
break;
default:
printf("Invalid choice try some other...");
break;
}
}while(choice != 3);
return 0;
}
struct node *create_circularlinkedlist(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n enter the data : ");
scanf("%d",&num);
while(num!=-1)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start == NULL)
{
new_node -> next=new_node;
start=new_node;
}
else {
ptr=start;
while(ptr -> next != start)
ptr=ptr -> next;
ptr -> next = new_node;
new_node -> next = start;
}
printf("\n Enter the data : ");
scanf("%d",&num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr -> next != start) {
printf("\t %d",ptr -> data);
ptr=ptr -> next;
}
printf("\t %d", ptr -> data);
return start;
}
struct node *insert_beg(struct node *start) {
struct node *new_node,*ptr;
int num;
printf("\n Enter the data : ");
scanf("%d",&num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> next !=start)
ptr = ptr -> next;
ptr -> next = new_node;
new_node -> next = start;
start = new_node;
return start;
}
struct node *insert_end(struct node *start) {
struct node *ptr, *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d",&num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> next != start)
ptr = ptr -> next;
ptr -> next = new_node;
return start;
}
struct node *delete_beg(struct node *start) {
struct node *ptr;
ptr = start;
while(ptr -> next!= start)
ptr = ptr -> next;
ptr -> next = start  -> next;
free(start);
start = ptr -> next;
return start;
}
struct node *delete_end(struct node *start) {
struct node *ptr,*preptr;
ptr = start;
while(ptr -> next != start)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
return start;
}
struct node *delete_after(struct node *start) {
struct node *ptr,*preptr;
int val;
printf("\n Enter the value after which the node has to deleted : ");
scanf("%d",&val);
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
if(ptr == start) {
start = preptr -> next;
}
free(ptr);
return start;
}
struct node *delete_list(struct node *start) {
struct node *ptr;
ptr = start;
while(ptr -> next != start)
start = delete_end(start);
free(start);
return start;
}
1. Create a list
2. Display the list
3. Exit the list
Enter your choice : 1
Enter -1 to end
Enter the data : 5
Enter the data : 1
Enter the data : 2
Enter the data : -1
Linked List Created Successfully
1. Create a list
2. Display the list
3. Exit the list
Enter your choice : 2
5   1   2
1. Create a list
2. Display the list
3. Exit the list
Enter your choice : 3

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you