0% found this document useful (0 votes)
190 views5 pages

Circular Linked List

Circular linked lists have no beginning or end, as the last node points back to the first. They are useful for tasks like maintaining a browser history, where pages can be visited in any order. Common operations on circular linked lists include insertion and deletion at the beginning, end, or a specific location within the list. Insertion involves adding a new node and adjusting pointers. Deletion finds and removes the target node, reconnecting the list by adjusting pointers of the previous and next nodes. Traversal for display starts at any node and follows pointers in a loop until returning to the starting node.

Uploaded by

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

Circular Linked List

Circular linked lists have no beginning or end, as the last node points back to the first. They are useful for tasks like maintaining a browser history, where pages can be visited in any order. Common operations on circular linked lists include insertion and deletion at the beginning, end, or a specific location within the list. Insertion involves adding a new node and adjusting pointers. Deletion finds and removes the target node, reconnecting the list by adjusting pointers of the previous and next nodes. Traversal for display starts at any node and follows pointers in a loop until returning to the starting node.

Uploaded by

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

Circular Linked List

In a circular linked list, the last node of the list contains a pointer to the first node of the list. We can have
circular singly linked list as well as circular doubly linked list.

We traverse a circular singly linked list until we reach the same node where we started. The circular singly
liked list has no beginning and no ending. There is no null value present in the next part of any of the
nodes.

The following image shows a circular singly linked list.

Circular linked list are mostly used in task maintenance in operating systems. There are many examples
where circular linked list are being used in computer science including browser surfing where a record of
pages visited in the past by the user, is maintained in the form of circular linked lists and can be accessed
again on clicking the previous button.

Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows.

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the circular linked list...

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, set head = newNode and newNode→next = head .

Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.

Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp →
next == head').
Step 6 - Set 'temp → next = newNode and newNode → next =head', head = newNode.

Inserting At End of the list


We can use the following steps to insert a new node at end of the circular linked list...

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL).

Step 3 - If it is Empty then, set head = newNode and newNode → next = head.

Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp →
next == head).

Step 6 - Set temp → next = newNode and newNode → next = head.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked list...
Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, set head = newNode and newNode → next = head.

Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.


Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want to
insert the newNode (until temp1 → data is equal to location, here location is the node value after
which we want to insert the newNode).

Step 6 - Every time check whether temp is reached to the last node or not. If it is reached to last node
then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the
function. Otherwise move the temp to next node.

Step 7 - If temp is reached to the exact node after which we want to insert the newNode then check
whether it is last node (temp → next == head).

Step 8 - If temp is last node then set temp → next = newNode and newNode → next = head.

Step 9 - If temp is not last node then set newNode → next = temp → next and temp →


next = newNode.

Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the circular linked list...

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible'.

Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize both
'temp1' and 'temp2' with head.

Step 4 - Check whether list is having only one node (temp1 → next == head)

Step 5 - If it is TRUE then set head = NULL and delete temp1

Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 → next == head )
Step 7 - Then set temp1 → next = temp2 → next, head = temp2 → next,  and delete temp2.

Deleting from End of the list


We can use the following steps to delete a node from end of the circular linked list...

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.

Step 4 - Check whether list has only one Node (temp1 → next == head)

Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function.
(Setting Empty list condition)

Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same
until temp1 reaches to the last node in the list. (until temp1 → next == head)

Step 7 - Set temp2 → next = head or Set temp2 → next =temp1-> next and delete temp1.


Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the circular linked list...

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.

Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node.
And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.

Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not
possible!!!'.

Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having
only one node (temp1 → next == head)

Step 7 - If list has only one node and that is the node to be deleted then set head = NULL and
delete temp1.

Step 08 - If temp1 is not first node and not last node then set temp2 → next = temp1 → next and
delete temp1.

Displaying a circular Linked List


We can use the following steps to display the elements of a circular linked list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last node
Step 5 - Finally display temp → data with arrow pointing to head → data.

You might also like