Unit-3.2 Circular Linked List
Unit-3.2 Circular Linked List
Unit-3.2
INTRODUCTION TO CIRCULAR LINKED LISTS
🞆 Circular Linked list is a linear collection of data
elements called nodes, where the linear order is
implemented by means of pointers (similar to
Circular Linked List)
🞆 The difference between Circular Linear Linked List
and Circular Linked List is that rather than storing
NULL in the link field of last node, we store address
of first node.
🞆 This creates a link between last node and first node
(in circular fashion)
CIRCULAR LINKED LIST
🞆 A circular linked list consists of nodes where each
node is connected to the next node by a Circular
link.
🞆 A linked list is represented by a pointer to the first
node of the linked list.
🞆 The first node is called head. If the linked list is
empty, then value of head is NULL.
🞆 A node in the circular linked list contains two fields-
⚫ a data field (which holds the element) and
⚫ a reference(link) field (that points to the next node in
the list).
🞆 End of the list is denoted by address of first node
in the reference field of last node.
STRUCTURE OF CIRCULAR LINKED
LIST
CREATION OF CIRCULAR LINEAR LINKED LIST
if(first==NULL);
first=new;
else
{
last->link=new;
last=new;
last->link=first;
}
}
DISPLAYING (TRAVERSING) LINKED LIST
🞆 Display function is used when we want to display contents of linked
list
void display()
{
if (first==NULL)
printf(“List is empty”);
else
{
struct node *temp=first;
printf(“The elements are..”);
do
{
printf(“%d”, temp->data);
temp=temp->link;
}
while (temp!=first)
}
}
INSERTION IN THE CIRCULAR LINKED LIST
🞆 Insertion is an operation that adds new
nodes in the existing list.
🞆 Insertion can be carried out in three different
cases-
⚫ Inserting a new node at the beginning of the list
⚫ Inserting a new node at the end of the list
⚫ Insertion of a new node anywhere in between
first and last node
INSERTING A NEW NODE AT THE
BEGINNING OF THE LIST
Steps-
🞆 Create a new node
new->link=first
⚫ Link field of new is stored with the address of first
node
first=new
⚫ Pointer first is made to point to the new node
last->link=first
⚫ Link field of last is stored with the address of first
node
EXAMPLE
firs las
t t
30 200 50 300 90 100
10 20 30
0 0 0
ne
w Circular Linked List before
Insertion
10 NULL
60
0
firs las
t t
60 10 20 30
0 0 0 0
Steps-
🞆 Create a new node
firs las
t t
10 100 30 200 50 300 90 600
60 10 20 30
0 0 0 0
Circular Linked List before ne
las
Insertion w
t
last ->link =
new 70 NULL
60
last = 0
new->link =
last 50
first 0
firs las
t t
10 100 30 200 50 300 90 50
60
60 0
10 20 30
0 0 0 0
ne
🞆 while(p<position) w
⚫ prev = cur
70 NULL
⚫ cur = cur -> link
50
0
EXAMPLE
🞆 position=3
70 NULL
50
🞆 prev -> link = new 0
ne
w
70 200
50
0
FUNCTION TO INSERT NEW ELEMENT
IN THE CIRCULAR LINKED LIST
void insert( )
{
struct node *prev, *cur, *temp;
int count=0, p=1;
int position, choice;
prev=NULL;
cur=first;
printf(“Enter the position of Insertion”);
printf(“1. At the beginning”);
printf(“2. At the end”);
printf(“3. In between”);
scanf(“%d”, &choice);
switch(choice)
{
case 1: //Insertion in the beginning
new->link = first;
first=new;
last->link=first;
break;
case 2: //Insertion at the end
last->link = new;
last=new;
last->link=first;
break;
case 3: //Insertion in between first & last node
Steps-
🞆 Alter the position of first node
🞆 Pointer first is made to point to the next
node in the list
🞆 Add the following statements-
first = first->link
⚫ Pointer first is made to point to the next node
last->link = first
⚫ Link field of last node is made to point to the first
node.
EXAMPLE
firs las
t t
10 20 30 40
0 0 0 0
firs las
t t
30 200 50 300 90 200
20 30 40
0 0 0
Circular Linked List after Deleting
first node
DELETING THE LAST NODE
🞆 Pointer last points to the last node in the list
Steps-
🞆 Alter the position of last node
🞆 Create temporary pointers Prev and Cur
🞆 Initially,
⚫ Prev = NULL and
⚫ Cur = first
firs cu
las
t r
t
10 100 30 200 50 300 90 500 70 NULL
60 50
0 0
Prev
CONTD..
🞆 Increment pointers (using loop) Prev and
Cur till –
⚫ pointer Prev points to the position before last
node and
firs ⚫ pointer Cur points to the position pre cu i.e
of last node
v r las
t reaches end of the list t
10 100 30 200 50 300 90 500 70 600
60 10 20 30 50
0 0 0 0 0
firs las
t t
10 100 30 200 50 300 90 600
60 10 20 30
0 0 0 0
🞆 while(p<position)
⚫ prev = cur Node to be
deleted
⚫ cur = cur -> link
EXAMPLE
🞆 position=3
firs Pre Cu las
t v r t
10 100 30 200 50 300 90 NULL
60 10 20 30
0 0 0 0
Node to be
🞆 prev -> link = cur -> link
deleted
Linked list before
deletion
firs las
t t
10 100 30 200 90 NULL
60 10 30
0 0 0
Linked list after
FUNCTION TO DELETE AN ELEMENT
IN THE CIRCULAR LINKED LIST
void delete( )
{
struct node *prev, *cur, *temp;
int count=0, p=1;
int position, choice;
prev=NULL;
cur=first;
if(first==NULL)
{
printf(“No elements in list”);
}
else
{
printf(“Enter the position of Deletion”);
printf(“1. Delete first node”);
printf(“2. Delete last node”);
printf(“3. Delete Intermediate node”);
scanf(“%d”, &choice);
switch(choice)
{
case 1: //Delete first node
printf(“The deleted node is:”, first->data); first=first-
>link;
break;