0% found this document useful (0 votes)
14 views16 pages

Module 2.3 Circular LL (Single &double)

Uploaded by

Shreeharsha N.L
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)
14 views16 pages

Module 2.3 Circular LL (Single &double)

Uploaded by

Shreeharsha N.L
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/ 16

CIRCULAR LINKED LISTS

1
CIRCULAR SINGLY LINKED LISTS
• In a circular linked list, the last node contains a pointer to the start node
of the list.
• While traversing a circular linked list, traversing can start from any node
and traverse the list forward until we reach the same node where started.
• It has no beginning and no ending.
• End node – identified as – node containing the address of start node is
actually the last node of list

• No NULL values in any next part of any node


• Drawback – complexity of iteration 2
• Circular linked lists are widely used in operating systems for task
maintenance.

• When surfing the Internet, the Back button and the Forward
button is used to move to the previous pages that is already visited,
which is done by using circular linked list

• It is used to maintain the sequence of the Web pages visited.

3
4
Operations on Circular Linked List:

• Inserting a new node –


Beginning and End Struct node
{
int data;
• Deleting a node – struct node *next;
Beginning and end };

5
NODE* insertfrontend(NODE *start, int n)
Inserting a Node at the {
NODE *new_node, *ptr;
beginning of Circular LL new_node = (NODE *) malloc(sizeof(struct node));
new_node ->data= n;
if(start== NULL) // If there is only one node in the list
{
start = new_node;
start->next = start;
}
else
{
ptr = start;
while(ptr->next != start)
{
ptr = ptr->next;
}
new_node->next=start;
ptr->next = new_node;// link the last node to start node
start = new_node;
}
return start;
6
}
NODE* insertend(NODE *start, int n)
Inserting a Node at the {
NODE *new_node, *ptr;
end of Circular LL new_node = (NODE *) malloc(sizeof(struct node));
new_node ->data = n;
if(start== NULL) // If there is only one node in the list
{
start = new_node;
start->next = start;
}
else
{
ptr = start;
while(ptr->next != start)
{
ptr = ptr->next;
}
ptr->next = new_node;// link the last node to start node
new_node->next=start;
}
return start;
} 7
Deleting a Node from front in a Circular Linked List
else // more than 1 nodes
NODE* deletefront(NODE *start) {
{ ptr = start;
NODE *ptr, *temp;
while(ptr->next != start)
if(start == NULL) // no node in list {
{ ptr = ptr->next;
printf(“ List Empty\n”); }
return start;
} temp = start;
start = start->next; // Make the second node as start
if(start->next == start) // only one node node
{ ptr->next = start; // Link last node to the new start node
printf(“Item deleted: %d\n",start->data);
free(start); printf(“Item deleted: %d\n",temp->data);
return NULL; free(temp);
} }
return start;
8
}
Deleting a Node from end in a Circular Linked List
NODE* deleteend(NODE *start)
{ else
NODE *ptr, *temp; {
ptr = start; // more than one node
if(start == NULL) // No nodes while(ptr->next != start)
{ {
printf(“ List Empty\n”); temp= ptr;
return start; ptr = ptr->next;
} }
if(start->next == start) // one node // Make the second last node as last node
{ temp->next = start;
printf(“Item deleted: %d\n",start- printf(“Item deleted: %d\n",ptr->data);
>data); free(ptr);
free(start); return start;
return NULL; }
} }

9
void display(NODE *start)
{
NODE *ptr;
Display Circular linked ptr= start;
List if (ptr == NULL)
{
printf("List is empty\n");
return;
}
printf("The list data are \n");
while(ptr->next != start)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
return;
} 10
CIRCULAR DOUBLY LINKED LISTS
• A circular doubly linked list contains a pointer to the next as well as the
previous node in the sequence.
• The circular doubly linked list does not contain NULL in the previous field of
the start node and the next field of the last node.
• Rather, the next field of the last node stores the address of the start node of the
list.
• Similarly, the previous field of the start field stores the address of the last node.

11
Inserting a Node at the beginning of Circular DLL
else
NODE* insertfront(NODE *start, int n) { // traverse till last node
{ ptr = start;
NODE *new_node, *ptr; while(ptr->rlink != start)
new_node = (NODE *) malloc(sizeof(struct node)); {
new_node ->data = n; ptr = ptr->rlink;
new_node ->rlink = NULL; }
new_node ->leftlink = NULL; ptr->rlink = new_node;
if(start == NULL) new_node ->leftlink = ptr;
{ new_node ->rlink = start;
start = new_node; start->leftlink = new_node ;
start->rlink=start; start = new_node ;
start->leftlink=start; return start;
} }
}

12
Inserting a Node at the end of Circular DLL

NODE* insertend(NODE *start, int n) else


{ { // traverse till last node
NODE *new_node, *ptr; ptr = start;
new_node = (NODE *) malloc(sizeof(struct node)); while(ptr->rlink != start)
new_node ->data = n; {
new_node ->rlink = NULL; ptr = ptr->rlink;
new_node ->leftlink = NULL; }
if(start == NULL) ptr->rlink = new_node;
{ new_node ->leftlink = ptr;
start = new_node; new_node ->rlink = start;
start->rlink=start; start->leftlink = new_node ;
start->leftlink=start; return start;
} }
}

13
Deleting a Node from front in a Circular DLL
// If there are more nodes
NODE* deletefront(NODE *start) else
{ {
NODE *ptr, *temp; ptr = start;
if(start == NULL) while(ptr->rlink != start)
{ {
printf(“ List Empty\n”); ptr= ptr->rlink;
return start; }
} temp = start;
// If there is only one node in the list start = start->rlink;
if((start->rlink == start)&&(start->leftlink == start)) ptr->rlink = start;
{ start->leftlink = ptr;
printf(“Item deleted: %d\n",start->data); printf(“Item deleted: %d\n",temp->data);
free(start); free(temp);
return NULL; return start;
} }
}
14
Deleting a Node from end in a Circular DLL
NODE* deleteend(NODE *start)
else
{
{// If there are more nodes
NODE *ptr, *temp;
ptr = start;
while(ptr->rlink != start)
if(start == NULL) // If there is no node in the list
{
{
temp= ptr;
printf(“ List Empty\n”);
ptr = ptr->rlink;
return start;
}
}
// If there is only one node in the list
temp->rlink = start;
if((start->rlink == start)&&(start->leftlink == start))
start->leftlink = temp;
{
printf(“Item deleted: %d\n",ptr->data);
printf(“Item deleted: %d\n",start->data);
free(ptr);
free(start);
return start;
return NULL;
}
}
}
15
void display(NODE *start)
Display circular DLL {
NODE *ptr;
ptr = start;
if (start == NULL)
{
printf("List is empty\n");
return;
}
printf("The list data are \n");
while(ptr->rlink != start)
{
printf("%d\n", ptr->data);
ptr = ptr->rlink;
}
printf("%d\n", ptr->data);

return;
} 16

You might also like