Unit-3 Circular Linked List
Unit-3 Circular Linked List
Dr.S.Bharathiraja,
Associate Professor
School of Computing Science and Engineering,
VIT – Chennai.
Topics
3. Circular Queue.
Circular Linked List
Definition:
without using null. For lists with a front and a back (such as a
queue), one stores a reference to the last node in the list. The next
node after the last node is the first node. Elements can be added to
the back of the list and removed from the front in constant time.
10 1000
1000
newnode
Circular Singly Linked List - Representation
struct node
{
int data;
node *link;
}*head=NULL,*newnode,*last,*delnode,*prev,*temp;
readnode()
{
Read newnode->data;
newnode->link=newnode;
}
Circular Single Linked List – Node
10 1000
1000
newnode
Creating a CSLL
create()
{
int c;
else
if(head!=NULL)
{
{
last->link=newnode;
print “Linked List is already created“;
last=last->link;
return(0);
} last->link=head;
else }
{ print “Press 1 to add another node:";
do read c;
{ }while(c==1);
getnode(); }
readnode(); return(0);
}
if(head==NULL)
{
head=last=newnode;
}
Circular Singly Linked List - Representation
10 1000
1000
delnode=head;
print”Deleted node is delnode->data;
head=head->link;
last->link=head;
free(delnode);
Deleted Node After Deletion
2000 3000
delnode
head Last
Deleting the last node in CSLL
else
int dellast() {
{ temp=head;
if(head==NULL)
while(temp->link!=NULL head)
{
{
printf("\nSLL is empty:");
prev=temp;
return(0);
temp=temp->link;
}
}
else if(head==last)
delnode=last;
{
print "\nDeleted node is delnode->data;
delnode=head;
last=prev;
print”Deleted node is delnode->data;
head=last=NULL; last->link=head;
free(delnode); free(delnode);
return(0); return(0);
} }
}
Deleting the last node in CSLL
Before Deletion
After Deletion
10 2000 20 1000
1000 2000
Deleted Node
head Last
30 1000
Deleting any node other than first and
last node in CSLL
Before Deletion
30 4000
3000
Deleting any node other than first and last node in CSLL
else
delmiddle() {
temp=head ;
{
print “Enter the element which has to be deleted";
int x; Read x;
if(head==NULL)
while(temp!=NULL last)
{ {
printf("\nSLL is empty:"); if(temp->data==x)
return(0); {
delnode=temp;
}
print “Deleted node is delnode->data;
else if(head==last) prev->link=temp->link;
{ free(delnode);
delnode=head; return(0);
}
print”Deleted node is delnode->data); else
head=last=NULL; {
free(delnode); prev=temp;
temp=temp->link;
return(0);
}
} }
}
}
Traversal and display
display()
{
if(head==NULL)
{
printf("\nCSLL is Empty");
return(0);
} 10 2000 20 3000 30 1000
temp=head;
1000 2000 3000
while(temp!=NULL last) head Last
{
Output:
print temp->data;
temp=temp->link; 10 20 30
}
print temp->data;
}
Traversal:
Visiting all the nodes in a linked list from head to last is called traversal
Advantages & disadvantages of Circular Linked
List
Advantages:
1. Each node in a circular linked list can be accessed from any node
Disadvantages
struct node
{
int data;
node *flink; // Forward Link field
node *blibk; // Backward Link field
}*head=NULL,*newnode,*last,*delnode,*prev,*temp;
readnode()
{
Read newnode->data;
newnode->flink=newnode;
newnode->blink=newnode;
}
Creating a Circular DLL
create() else
{ {
int c; getnode();
if(head!=NULL)
readnode();
{
last->flink=newnode;
print”Linked List is already created“;
return(0);
newnode->blink=last;
} last=last->flink;
else last->flink=head;
{
head->blink=last;
do
}
{
if(head==NULL)
print”Press 1 to add another node:”;
{ read c;
getnode(); }while(c==1);
readnode(); }
head=last=newnode; return(0);
} }
Creating Circular Doubly Linked List
head Last
Insert Last in Circular DLL
insertlast()
{
getnode();
if(newnode==NULL)
{
printf"No Memory";
return(0);
}
readnode();
if(head==NULL)
{
head=last=newnode;
return(0);
}
last->flink=newnode;
newnode->blink=last;
last=last->flink;
last->flink=head;
head->blink=last;
Insert Last in Circular DLL
Before Insertion
After Insertion
Before Insertion
After Insertion
2000 50 3000
4000
newnode
Delete First in Circular DLL
delfirst()
{
if(head==NULL)
{
print”DLL is empty:";
return(0);
}
else if(head==last)
{
delnode=head;
print "Deleted node is delnode->data;
head=last=NULL;
free(delnode);
return(0);
}
else
{
delnode=head;
print "Deleted node is delnode->data;
head=head->flink;
last->flink=head;
head->blink=last;
Delete First in Circular DLL
Before Deletion
After Deletion
Null 10 2000
int dellast()
{
Delete Last in Circular DLL
if(head==NULL)
{
printf("\nDLL is empty:");
return(0);
}
else if(head==last)
{
delnode=head;
print” Deleted node is ,delnode->data;
head=last=NULL;
free(delnode);
return(0);
}
else
{
delnode=last;
print "Deleted node is ,delnode->data;
last=last->blink;
last->flink=head;
Delete Last in Circular DLL
Before Deletion
After Deletion
1000 2000
head Last Deleted Node
2000 30 Null
Delete Middle in Circular DLL
int delmiddle()
{ while(temp!=last)
int x; {
if(head==NULL) if(temp->data==x)
{ {
printf("\nDLL is empty:"); delnode=temp;
return(0); next=temp->flink;
} prev=temp->blink;
else if(head==last) prev->flink=next;
{ next->blink=prev;
delnode=head; print”Node deleted is delnode->data;
print”Deleted node is delnode->data; free(delnode);
head=last=NULL; return(0);
free(delnode); }
return(0); else
} {
else temp=temp->flink;
{
}
}
temp=head;
}
print"Enter the element which has to be
deleted"; }
read x;
Delete Middle in Circular DLL
Before Deletion
2000 30 4000
Delnode
Traversal and display
display()
{
if(head==NULL)
{
printf("\nDLL is
Empty");
return(0);
}
temp=head;
while(temp!=last)
{
print temp->data;
temp=temp->flink;
}
print temp->data;
}
Circular Queue Operations
Declarations:
struct node
{
int data;
node *link;
}*rear,*front=NULL,*delnode,*newnode,*temp;
Creating a new node
getnode()
{
newnode=(node*)malloc(sizeof(node));
}
readnode()
{
read newnode->data;
newnode->link=newnode;
}
enqueue()
Enqueue in Circular Queue
{
getnode();
if(newnode==NULL)
{
print ”Memory insufficient Queue is full";
return(0);
}
else
{
readnode();
if(front==NULL)
{
front=rear=newnode;
return(0);
}
rear->link=newnode;
rear=newnode;
rear->link=front;
printf "The new node inserted is , rear->data;
return(0);
}
Enqueue in Circular Queue
Before Enqueue
10 2000 20 1000
1000 2000
front rear
After Enqueue
if(front==NULL)
{
print “Deletion is not possible Queue is empty";
return( );
}
else
{
if(front==rear)
{
delnode=front;
print "Deleted node is ,delnode->data;
free(delnode);
front=rear=NULL;
return( );
}
delnode=front;
print "Deleted node is ,delnode->data;
front=front->link;
Dequeue in Circular Queue
Before Dequeue
Before Dequeue
20 3000 30 2000
2000 3000
front rear delnode
10 2000
1000
Display in Circular Queue
int display()
{
if(front==NULL)
{
print “Display is not possible Queue is empty";
return(0);
}
temp=front;
while(temp!=last)
{
print temp->data;
temp=temp->link;
}
print temp->data;
return(0);
}
1.Consider a train passing from Chennai to Madurai has 3
coaches namely S1, S2 and S3. The details of the passengers in
each coach are,
Coach No Total seats Type
S1 60 SL
S2 70 SL
S3 65 2S