Circular Single Linked Lists - Introduction and Implementation


Circular linked lists may be circular single linked lists or circular double linked lists. Read this article for a brief introduction of circular single linked lists and the C programs to implement circular single linked lists.

Circular Single Linked Lists


If in a single linked list, the last node is connected to the first node, it is called a circular single linked list. The operations on circular single linked lists are similar to that of single linked lists i.e. insertion, deletion, display etc,. The last element contains some data and pointer to the first element but not a NULL pointer.

C Program to implement Circular Single Linked List


Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL,*tail=NULL;
struct node *create_node(int data)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void create_clinkedlist()
{
head=create_node(0);
tail=create_node(0);
head->next=tail;
tail->next=head;
}
void insert_node(int data)
{
struct node *newnode,*temp;
newnode=create_node(data);
temp=head->next;
head->next=newnode;
newnode->next=temp;
}
void delete_node(int data)
{
struct node *temp0,*temp1;
if(head->next==tail&&tail->next==head)
printf("\n Circular Single Linked List is Empty");
temp0=head;
temp1=head->next;
while(temp1!=tail)
{
if(temp1->data==data)
{
temp0->next=temp1->next;
free(temp1);
break;
}
temp0=temp1;
temp1=temp1->next;
}
return;
}
void display()
{
struct node *temp=head;
int i=0;
printf("\n Circular Single Linked List is : ");
while(temp)
{
printf("%d ",temp->data);
if(temp->next==head)
break;
temp=temp->next;
i++;
}
}
void main()
{
int choice,data;
clrscr();
create_clinkedlist();
while(1)
{
printf("\n\n Circular Single Linked List Operations");
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the value of node to insert : ");
scanf("%d",&data);
insert_node(data);
break;
case 2:
printf("\n Enter the value of node to delete : ");
scanf("%d",&data);
delete_node(data);
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n Your choice is invalid");
break;
}
}
}

Output
Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter the value of node to insert : 1

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter the value of node to insert : 3

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter the value of node to insert : 5

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter the value of node to insert : 7

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter the value of node to insert : 9

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
Circular Single Linked List is : 0 1 3 5 7 9 0

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : 9

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : 7

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : 5

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : 3

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
Circular Single Linked List is : 0 1 0

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : 1

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Enter the value of node to delete : -1
Circular Single Linked List is Empty

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 5
Your choice is invalid

Circular Single Linked List Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 4


Related Articles

Write A C Program To Implement Stack Using Array

In this program we have to implement the stack operation by using the arrays. Here the stack operations are push and pop. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements from a stack

More articles: Programming

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: