0% found this document useful (0 votes)
2K views

Circular Linked List

The document discusses circular linked lists which differ from linear linked lists in that the last node points to the first node, forming a loop. There are two types - singly circular where each node has one pointer and doubly circular where each node has two pointers. Circular lists allow traversing the entire list from any starting point and are useful for applications that require repeatedly traversing a list. Common operations like insertion, deletion and traversal are described along with code snippets to implement them.

Uploaded by

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

Circular Linked List

The document discusses circular linked lists which differ from linear linked lists in that the last node points to the first node, forming a loop. There are two types - singly circular where each node has one pointer and doubly circular where each node has two pointers. Circular lists allow traversing the entire list from any starting point and are useful for applications that require repeatedly traversing a list. Common operations like insertion, deletion and traversal are described along with code snippets to implement them.

Uploaded by

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

Circular linked list

Content:
• Circular linked list
• Difference b/w linear linked list and circular
linked list
• Types of circular linked list
• Advantages
• Disadvantages
• Operations on circular linked list
What is Circular linked list?

• Circular linked list is a variation of linked list in


which the first element points to the next
element and the last element points to the
first element.
• Both singly and doubly linked list can be made
into a circular linked list.
• Circular linked list can be used to help traverse
the same list again and again if needed.
Circular linked list vs. Linear linked list
• A circularly linked list may be a natural option to represent arrays
that are naturally circular, e.g. the corners of a polygon, a pool
of buffers that are used and released in FIFO order, or a set of
processes that should be time-shared. In these applications, a pointer
to any node serves as a handle to the whole list.
• With a circular list, a pointer to the last node gives easy access also to
the first node, by following one link. Thus, in applications that
require access to both ends of the list, a circular structure allows one
to handle the structure by a single pointer, instead of two.
• The simplest representation for an empty circular list is a null
pointer, indicating that the list has no nodes. Without this choice,
many algorithms have to test for this special case, and handle it
separately. By contrast, the use of null to denote an empty linear
list is more natural.
Types of Circular linked list:
1. Singly: The last node points to the first node and there is only
one link between the nodes of linked list.

2. Doubly: The last node points to the first node and


there are two links between the nodes of linked list.
Advantages of Circular linked lists:
1. Any node can be a starting point. We can traverse the whole
list by starting from any point. We just need to stop when
the first visited node is visited again.

2. Circular lists are useful in applications to repeatedly go


around the list. For example: when multiple applications
are running on a PC, it is common for the operating system
to put the running applications on a list and then to cycle
through them, giving each of them a slice of time to execute,
and then making them wait while the CPU is given to
another application. It is convenient for the operating
system to use a circular list so that when it reaches the end
of the list it can cycle around to the front of the list.

3. Circular Doubly Linked Lists are used for implementation


of advanced data structures like Fibonacci Heap.
Disadvantages of Circular linked list:
1. Depending on the implementation, inserting at
start of list would require doing a search for
last node which could be expensive.

2. Finding end of the list and loop control is


harder ( no NULL’s to mark the beginning and
end).
Operations on singly circular
linked list:
• Insertion
• Deletion
• Traversing
Insertion:
• Insertion can be of three types.
▫ Insert at first
▫ Insert at last
▫ Insert after mid

• Note: Insertion after mid in circular and


linear linked list is exactly same.
Insertion at first
Algorithm Program
• Start. void addfront()
• Declare struct node *t. {
• Set t:=start. struct node *t=start;
• Create a new node n
and enter the struct node *n=new
information in info
node;
part.
• Check if start=NULL
printf(“\nenter the
set start=n
information”);
set start->next=start. scanf(“%d”,&n->info);
else
• Set n->next=start.
Algorithm Program
• Repeat step(a) if(start==NULL)
while(t->next!=start) {
▫ (a) set t:=t- start=n;
>next. start->next=start;
• [end of loop] }
• Set t->next=n. else
{
• Set start=n. [end if]
n->next=start;
• Stop. while(t->next!=start)
t=t->next;
t->next=n;
start=n;
}
Insert at last
Algorithm Program
• Start. void addlast()
• Declare struct node *t. {
• Set t:=start. struct node *t=start;
• Create a new node n
and enter the
information in info struct node *n= new node;
part.
• Check if start=NULL printf(“\nenter the
then, information”);
set start:=n. scanf(“%d”,&n->info);
set start->next:=start.
Otherwise
• Set n->next:=start.
Algorithm Program
• Repeat step(a) if(start==NULL)
while(t->next!=start) {
▫ (a) set t:=t->next. start=n;
• [end of loop] start->next=start;
• Set t->next=n. }
else
[end if] {
• Stop. n->next=start;
while(t-next!=start)
t=t->next;
t->next=n;
}
Deletion:
• Deletion can be of three types.
▫ Delete from front
▫ Delete from last
▫ Deletion from mid

• Note: Deletion from mid in circular and


linear linked list is exactly same .
Delete from front
Algorithm Progra
• Start. m
void delfront()
• Set t:= start. {
• Check if (start=NULL) struct node *t=start;
then, if (start==NULL)
print “empty list” printf (“\nempty list”);
• Check if else if (start->next==start)
(start->next=start) {
then, free (t);
free (t) start=NULL;
set start:=NULL }
otherwise else
• Repeat step(a) {
while(t->next!=start) while(t-
▫ (a) set t:=t->next >next!
• [end of loop] =start)
t=t->next;
Algorithm Program
• set start:=start->next start=start->next;
• Free(t->next). free(t->next);
• Set t->next:=start. t->next=start;
• [end of if] }
• stop }
Delete from last
Algorithm Program
• Start. void dellast()
• Set t:=start. {
• Check if (start=NULL) then, struct node *t=start;
if (start==NULL)
print “empty list”
• Check if (start->next=start) printf(“\nempty list”);
then, else if (start->next==start)
{
free (t) free (t);
Set start:=NULL start=NULL ;
otherwise }
• Repeat step(a) else
while(t->next- {
>next!=start) while(t-
▫ (a) set t:=t- >next-
>next >next!
• [end of loop] =start)
• Free(t->next). t=t->next;
• Set t- free(t->next);
>next:=start. t->next=start;
Traversing
Algorithm Progra
• Start. m
void display()
• Set node *t:=start. {
• Check if(start=NULL) struct node *t=start;
if(start=NULL)
then, print “empty list” printf (“\nempty list”);
otherwise else
• Repeat step a and b {
while(t->next!=start) while(t->next!=start)
▫ (a) print t->info {
▫ (b) set t:=t->next printf(“%d”, t-
• [end of loop] >info); t=t->next;
}
• Print t->info [end if] printf(“%d”, t-
• stop >info);
}
}
Thank you

You might also like