0% found this document useful (0 votes)
22 views35 pages

Unit-3.2 Circular Linked List

Uploaded by

parthskrpp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views35 pages

Unit-3.2 Circular Linked List

Uploaded by

parthskrpp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CIRCULAR LINKED LISTS

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

🞆 Creation is used only when we create new


nodes in the list.
Note-
1. We maintain some pointers-
1. new- addition of new node in the list
2. first- points to first node in the list
3. last- points to last node in the list
2. Initially first and last both points to NULL
3. If there is only one node in the list, first and
last both points to the same node
4. Whenever there is addition of new node in the
list, last pointer node is updated.
FUNCTION TO CREATE A NEW NODE
void create()
{
strcut node *new=malloc(size of (structnode));;
printf(“Enter the item to be created”);
scanf(“%d”, &new->data);
new->link=NULL;

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

🞆 Pointer first points to the first node in the list

Steps-
🞆 Create a new node

🞆 Alter the position of first node

🞆 Add the following statements-

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

10 100 30 200 50 300 90 100

60 10 20 30
0 0 0 0

Circular Linked List after Inserting new node at the


INSERTING A NEW NODE AT THE END OF
THE LIST

🞆 Pointer last points to the last node in the list

Steps-
🞆 Create a new node

🞆 Alter the position of last node

🞆 Add the following statements-

last ->link = new


⚫ Link field of last is stored with the address of new
node
last = new
⚫ Pointer last 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
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

Circular Linked List after Inserting new node


firs las
t t
10 100 30 200 50 300 90 500 70 60
60 0
10 20 30 50
0 0 0 0 0
Circular Linked List after Inserting new node
at the end
INSERTING A NEW NODE IN BETWEEN
FIRST AND LAST NODE OF THE LIST
🞆 Here the insertion is based on the position.
🞆 Ask user, the position of the node where it is to be
inserted (say n)
🞆 In this case we consider two additional temporary
pointer variables-
⚫ Prev (stands for previous node i.e node (n-1))
⚫ Cur(stands for current node i.e node (n))
🞆 Initially,
⚫ Prev = NULL and
⚫ Cur = first
🞆 Increment pointers (using loop) Prev and Cur till –
⚫ pointer Prev points to the position before new node is to be
inserted (i.e position n-1) and
⚫ pointer Cur points to the position where new node is to be
inserted (i.e position n) or reaches end of the list (i.e last-
>link=first)
EXAMPLE
🞆 Count number of nodes already present in the list
🞆 Take value of position<=no. of nodes
🞆 Consider position=3
🞆 Initially, Prev = NULL and Cur = first (p=1)
Cu firs las
r t t
10 100 30 200 50 300 90 600
60 10 20 30
0 0 0 0

ne
🞆 while(p<position) w
⚫ prev = cur
70 NULL
⚫ cur = cur -> link
50
0
EXAMPLE
🞆 position=3

firs Pre Cu las


t v r t
10 100 30 200 50 300 90 600
60 10 20 30
0 0 0 0
ne
w

70 NULL

50
🞆 prev -> link = new 0

🞆 new -> link = Cur


EXAMPLE
🞆 position=3

firs Pre Cu las


t v r t
10 100 30 500 50 300 90 600
60 10 20 30
0 0 0 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

//count number of nodes in the list.


while(temp!=NULL)
{
temp=temp->link;
count=count+1;
}
printf(“Enter the position between node-1
and node-%d”, count);
scanf(“%d”, &position);
while(p<position)&&(cur!=NULL)
{
prev=cur;
cur=cur->link;
p++;
}
if(cur == position)
{
prev->link=new; new->link=cur;
}
else
printf(“Wrong choice of position”);
break;
}
}
DELETION IN THE CIRCULAR LINKED
LIST
🞆 Deletion is an operation to remove a node from
an existing list.
🞆 A node can be deleted in three different ways-
⚫ Deleting the first node
⚫ Deleting the last node
⚫ Deletion based on the position in between first and
last node
DELETING THE FIRST NODE

🞆 Pointer first points to the first node in the list

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 100 30 200 50 300 90 100

10 20 30 40
0 0 0 0

Circular Linked List before


Deletion

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

Circular Linked List before


Deletion
EXAMPLE

Add following statements


prev -> link= first
last = prev

firs las
t t
10 100 30 200 50 300 90 600
60 10 20 30
0 0 0 0

Circular Linked List after deleting


last node
DELETING THE INTERMEDIATE NODE
🞆 Here the deletion is based on the position.
🞆 Ask user, the position of the node which is to be
deleted (say n)
🞆 In this case we consider two additional temporary
pointer variables-
⚫ Prev (stands for previous node i.e node (n-1))
⚫ Cur(stands for current node i.e node (n))
🞆 Initially,
⚫ Prev = NULL and
⚫ Cur = first
🞆 Increment pointers (using loop) Prev and Cur till –
⚫ pointer Prev points to the position before the node to be
deleted (i.e position n-1) and
⚫ pointer Cur points to the position of the node to be
deleted (i.e position n) or reaches end of the list (i.e
NULL)
EXAMPLE
🞆 Count number of nodes already present in the list
🞆 Take value of position<=no. of nodes
🞆 Consider position=3
🞆 Initially, Prev = NULL and Cur = first (p=1)
Cu firs las
r t t
10 100 30 200 50 300 90 NULL
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;

case 2: //Delete last node


printf(“The deleted node is:”, first->data);
if(first==last)
first=last=NULL;
else
{
while(cur!=last)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
last=prev;
}
break;
case 3: //Deleting Intermediate node

//count number of nodes in the list.


while(temp!=NULL)
{
temp=temp->link;
count=count+1;
}

printf(“Enter the position between


node-1 and node-%d”, count);
scanf(“%d”, &position);
while(p<position) && (cur!=NULL)
{
prev=cur;
cur=cur->link;
p++;
}
if(cur == position)
{
printf(“Enter deleted element is:”,
cur->data);
prev->link=cur->link;
}
else
printf(“Wrong choice of position”);
break;
}
}
SEARCHING IN CIRCULAR LINKED LIST
🞆 Searching is an operation that
finds(searches) the required element in the
list
🞆 In Linked List, we search data sequentially i.e
starting from first node to the last node
(same as Linear Search)
🞆 Below functions helps to search the required
element in the list
FUNCTION TO SEARCH AN ELEMENT IN THE LIST
void search()
{
int element, position=1;
if (first==NULL)
printf(“There are no elements in the list”);
else
{
struct node*temp=first;
printf(“Enter the element to search”);
scanf(“%d”, &element);
do
{
temp=temp->link;
position++;
}
while((temp!=first) || (temp->data!=element))
if (temp->data==element)
printf(“The element is present at the position=%d”, position)
else
printf(“Element is not present in the list”);
}

You might also like