0% found this document useful (0 votes)
9 views

Unit-3 Circular Linked List

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-3 Circular Linked List

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

CIRCULAR LINKED LIST

Dr.S.Bharathiraja,
Associate Professor
School of Computing Science and Engineering,
VIT – Chennai.
Topics

1. Circular Singly Linked List.

2. Circular Doubly Linked List.

3. Circular Queue.
Circular Linked List
Definition:

• In a circularly linked list, all nodes are linked in a continuous circle,

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.

• Circularly linked lists can be either singly or doubly linked.


Circular Linked List
Node:
A node is a memory location which contains
data and address. A Node in a single Linked
List contains 2Fields namely:
1.Data Field – To store the data.
2.Address Field – To store the address of the
next node.

Data Field Address Field


Types of Circular Linked List

1. Circular Singly Linked List

2. Circular Doubly Linked List


Circular Singly Linked List

 It’s a Dynamic Data structure.


 A node in a circular singly linked list has two fields
namely:
Data Field – For holding the data
Linked Field – For holding the address of next node
 There exists a single link between each node.
 The first node is indicated using a head pointer and the
last node is indicated using a last pointer.
 The last nodes link field is filled with address of first
node. (Non-Terminating Linked List).
 Traversal in a CSLL is possible only in one direction(From
head to last).
Circular Single Linked List – Node

Data Field Address Field

10 1000

1000

newnode
Circular Singly Linked List - Representation

10 2000 20 3000 30 4000 40 1000

1000 2000 3000 4000


Circular Linked List
Operations carried out in a Circular Linked List :

1.Creating a Circular Linked List – Creation( )


2.Inserting a node on to circular Linked List – Insert( )
3.Deleting a node from circular Linked List – Delete( )
4.Modifying elements from circular Linked List – Modify( )
5.Displaying the elements of a circular Linked List –
isplay( )
6.Finding an element from the Display( ) – Find( )
Declaration for a CSLL

struct node
{
int data;
node *link;
}*head=NULL,*newnode,*last,*delnode,*prev,*temp;

Where node is a user defined data type which is


capable of holding data and address of next node.
Circular Singly Linked List
 To create a new node in a CSLL we can define two basic
functions
getnode( ) – for allocating the memory for a
node dynamically.
readnode( )- for reading data and assigning the
address of newnode in the link field.

 Whenever we need to create a new node we can call the


functions getnode( ) and readnode( ).
Creating a new node
getnode()
{
newnode=(node*)malloc(sizeof(node));
}

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

head Last newnode


last->link=newnode;
last=last->link;
10 2000 20 1000
last->link=head;
1000 2000
newnode Last
head

10 2000 20 3000 30 10000

1000 2000 3000


head Last
Inserting as last node in CSLL
insertlast()
{
getnode();
if(newnode==NULL)
{
print “Memory Insufficient";
return(0);
}
readnode();
Before Insertion
if(head==NULL)
10 2000 20 3000 30 1000
{
head=last=newnode;
return(0);
1000 2000 3000
} head Last
last->link=newnode;
last=last->link;
last->link=head; After Insertion
}
10 2000 20 3000 30 4000 40 1000

1000 2000 3000 4000


head Last
int insertfirst()
Inserting as First node in CSLL
{
getnode();
if(newnode==NULL)
{
print “No Memory";
return(0);
} Before Insertion
readnode();
10 2000 20 3000 30 1000
if(head==NULL)
{
1000 2000 3000
head=last=newnode;
head Last
return(0);
}
newnode->link=head;
After Insertion
head=newnode;
last->link=head;
}
40 1000 10 2000 20 3000 30 4000

4000 1000 2000 3000


head Last
Insert Middle operation in a CSLL
Before Insertion

10 2000 20 3000 30 10000

1000 2000 3000


head temp Last
if(temp->data==x)
{
newnode->link=temp->link;
temp->link=newnode;
return(0);
} After Insertion – where x=20

10 2000 20 4000 30 1000


40 3000
1000 2000 3000
head 4000 Last
temp
newnode
Insert Middle operation in a CSLL
insertmiddle()
{
int x; while(temp!=NULL last)
getnode(); {
if(newnode==NULL) if(temp->data==x)
{
{
newnode->link=temp->link;
printf("\nNo Memory"); temp->link=newnode;
return(0); return(0);
} }
readnode(); else
if(head==NULL) temp=temp->link;
}
{
return(0);
head=last=newnode; }
return(0);
}
print ”Enter the node after which you want to
insert the new node:";
read x;
temp=head ;
Deleting the First node in CSLL
delfirst() else
{ {
if(head==NULL) delnode=head;
print”Deleted node is delnode->data;
{
head=head->link;
print ”CSLL is empty:";
return(0);
last->link=head;
free(delnode);
} return(0);
else if(head==last) }
{ }
delnode=head;
print”Deleted node is delnode->data;
head=last=NULL;
free(delnode);
return(0);
}
Deleting the First node in CSLL
Before Deletion

10 2000 20 3000 30 1000

1000 2000 3000


head Last
delnode

delnode=head;
print”Deleted node is delnode->data;
head=head->link;
last->link=head;
free(delnode);
Deleted Node After Deletion

10 2000 20 3000 30 2000

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

10 2000 20 3000 30 1000

1000 2000 3000


head Last

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

10 2000 20 3000 30 4000 40 1000

1000 2000 3000 4000


head Last

Before Deletion – where x=30

10 2000 20 4000 40 1000

1000 2000 4000


head Last Deleted Node

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

whereas a singly linked list can be accessed only by staring from

the first node.

2. No need of NULL pointer as all pointer contain valid addresses.

Disadvantages

1. An infinite looping can be caused while traversing it.


Lab Exercise: 04-06-20

1.Create a menu driven program to perform the


following operations in a circular singly linked list.
a. Create()
b. Insert()
c. Delete()
d. Search()
e. Display()
Circular Doubly Linked List
 It’s a Dynamic Data structure.
 A node in a Doubly linked list has three fields namely:
Data Field – For holding the data
Forward link Field – For holding the address of next node
Backward link field – for holding the address of prev node
 There exists two link between each node. (Forward and backward
link for each node)
 The first node is indicated using a head pointer and the last node is
indicated using a last pointer.
 The last nodes flink field is filled with address of first node and the
head nodes blink is filled with address of last node. (Non-
Terminating Linked List)
 Traversal in a DLL is possible both direction(From head to last as
well as from last to head).
Doubly Linked List – Node

Forward Data Field Backward


Address Field Address Field

1. Data Field – For holding the data

2.Forward link Field – For holding the address of next node

3.Backward link field – for holding the address of prev node


Circular Doubly Linked List - Representation

3000 10 2000 1000 20 3000 2000 30 1000

1000 2000 3000


head Last
Declaration for Circular DLL

struct node
{
int data;
node *flink; // Forward Link field
node *blibk; // Backward Link field
}*head=NULL,*newnode,*last,*delnode,*prev,*temp;

Where node is a user defined data type which is capable of


holding data and 2addresses one for storing the next node’s
address and other one for storing the previous node’s
address
Circular Doubly Linked List
 To create a new node in a DLL we can define two basic
functions
getnode( ) – for allocating the memory for a
node dynamically.
readnode( )- for reading data and assigning a NULL
value in link field.

 Whenever we need to create a new node we can call the


functions getnode( ) and readnode( ).
Creating a new node
getnode()
{
newnode=(node*)malloc(sizeof(node));
}

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

3000 10 2000 1000 20 3000 2000 30 1000

1000 2000 3000

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

2000 10 2000 1000 20 1000

1000 head 2000 Last

After Insertion

3000 10 2000 1000 20 3000 2000 30 1000

1000 2000 3000


head Last
Insert First in Circular DLL
int insertfirst()
{
getnode();
if(newnode==NULL)
{
print”No Memory";
return(0);
}
readnode();
if(head==NULL)
{
head=last=newnode;
return(0);
}
newnode->flink=head;
head->blink=newnode;
head=newnode;
last->flink=head;
head->blink=last;
Insert First in Circular DLL

Before Insertion

2000 10 2000 1000 20 1000

1000 head 2000 Last

After Insertion

2000 30 1000 3000 10 2000 1000 20 3000

3000 1000 2000


head Last
Insert Middle in Circular DLL
int insertmiddle()
{ while(temp!=last)
int x; {
getnode(); if(temp->data==x)
if(newnode==NULL) {
{ next=temp->flink;
print “No Memory“; newnode->flink=next;
return(0); next->blink=newnode;
} temp->flink=newnode;
readnode(); newnode->blink=temp;
if(head==NULL)
{ return(0);
head=last=newnode;
}
return(0);
else
}
temp=temp->flink;
print”Enter the node after which you
want to insert the new node:"; }
read x; return(0);
temp=head; }
Insert Middle in Circular DLL
Before Insertion

3000 10 2000 1000 20 3000 2000 30 1000

1000 head 2000 Last 3000

After Insertion – Where x=20

3000 10 2000 1000 20 4000 4000 30 1000

1000 2000 3000


head prev next Last

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

3000 10 2000 1000 20 3000 2000 30 1000

1000 head 2000 Last 3000

After Deletion

3000 20 3000 2000 30 2000

head 2000 Last 3000


Deleted Node

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

3000 10 2000 1000 20 3000 2000 30 1000

1000 head 2000 Last 3000

After Deletion

2000 10 2000 1000 20 1000

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

4000 10 2000 1000 20 3000 2000 30 4000 3000 40 1000

1000 2000 3000 4000


head Last

After Deletion – Where X = 30

Null 10 2000 1000 20 4000 2000 40 NULL

1000 2000 4000


head Last
Deleted Node

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

10 2000 20 3000 30 1000

1000 2000 3000


front rear
dequeue() Dequeue in Circular Queue
{

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

10 2000 20 3000 30 1000

1000 2000 3000


front rear

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

Perform the following operations by using doubly linked list


data structure
(i)Insert a coach in between S2 and S3
(ii)Insert a coach in front and at the end of the train
(iii)Remove a coach
1.During festival season, Indian Railway has decided to add additional
coaches in a particular train. As of now there are only 10 coaches
available in the train and the compartments are namely 4 sleeper
classes, 2 A/C class, 2 pantries & 2 unreserved coach.
Coach Type Coach No’s
Engine 0
A/C Class 1-2
Sleeper 5-8
Pantry 13-14
Unreserved 17-18
All the compartments are numbered in the order as mentioned above
starting from the Engine.
(i)Write an algorithm using Linked list to add a new A/C and Sleeper
coaches in such a way that all sleeper class coaches come together and
all A/C coaches come together.
(ii) The TTR wants to check for a coach No whether it is attached or not.
Write appropriate algorithm to help the TTR to identify a coach No.
2. Given two Linked Lists, create union and
intersection lists that contain union and
intersection of the elements present in the given
lists. Order of elements in output lists doesn’t
matter.
Example:
Input: List1: 10->15->4->20
List2: 8->4->2->10
Output:
Intersection List(L1 ⋂ L2): 4->10
Union List(L1 ⋃ L2): : 10->15->4->20->8->2
8->4->2->10->15->20
3. Given a linked list, reverse alternate nodes
and append them to end of list.
Examples
Input List: 1->2->3->4->5->6
Output List: 1->3->5->6->4->2

Input List: 12->14->16->18->20


Output List: 12->16->20->18->14

You might also like