In this blog post will study what is circular linked lists, its
applications, operations and implementation in C++.
Please check the blog post if you don't know what is
Linked Lists.
Definition
Similar to singly linked lists, circular linked lists are also
one way list but the difference is that the last node contains
the reference to the head node making it a circular.
CLL do not have ends that is the last node reference field
isn't NULL. While traversing the circular linked lists we
should be careful, otherwise we will be traversing the list
infinitely.
In circular linked lists, each node has a successor which
means to say that every node contains the link to next node.
Applications
- To implement circular queue.
-
Implementation of Advanced data structures like Fibonacci
Heap.
-
Used by the Operating system to share time for different
users, generally uses a Round-Robin time-sharing mechanism.
-
Multiplayer games use a circular list to swap between players
in a loop.
- Music playlist which repeats endlessly playing songs.
Operations
Do refer the code available the end of this section to
understand the following thoery.
Note that the head node's data is used to count the no. of nodes
in the list & it's next pointer points to the first node.
1. Traverse: Iterate through the nodes in the
linked list starting from the head node.
-
Let us assume that the head points to the first node of the
list.
-
Create a temporary node which will point to the same node as
that of head.
-
Follow the pointers and stop when the next pointer points to
head.
2. Append: Add a new node to the end of a
list.
-
Create a new node and initially keep its next pointer
pointing to itself.
-
The new node will be placed just after the tail node (last
node of the list), which means it will have to be inserted
in between the tail node and the first node.
-
Traverse to the last node and change the next pointer, to
point to the new node.
- Change the new node's next pointer, to point to head.
3. Prepend: Add a new node to the beginning
of a list.
-
Create a new node and initially keep its next pointer
pointing to itself.
-
Update the next pointer of new node, to point to the current
head's next node.
- Update head's next pointer to point to the new node.
4. Insert by position : Add a new node to a
specific position on the list.
-
If a node has to be inserted at a given position then we
need to modify two next pointers.
-
If we want to add an element at position 3 then we stop at
position 2. That means we traverse 2 nodes and insert the
new node. For simplicity let us assume that the second node
is called position node.
-
Make the new node's next pointer point to the the next
pointer of position node.
-
Change the next pointer of the position node, to point to
the new node.
5. Delete at front: Remove the first node
from the list.
-
Traverse and reach the tail node. Update the tail node's
next pointer to point to next node of head
- Store the first node in temporary node pointer.
-
Now, move the head node's pointer to the next node of
temporary node and the first node will be unlinked with
list.
5. Delete at rear: Remove the last node from
the list.
-
We need to find the node which is previous of the last node,
so traverse the list and while traversing maintain the
previous node address also.
-
By the time we reach the end of the list, we will have two
pointers, one pointing to the tail node and the other
pointing to the node before the tail node.
-
Update previous node’s next pointer to point to head and the
last node will be unlinked with the list.
5. Delete by position: Remove a node at
specified position from the list.
-
In this case, the node to be removed is always located
between two nodes. Head and tail links are not updated in
this case and such a removal can be done in two steps
-
Similar to the previous case, maintain the previous node
while traversing the list. Once we find the node to be
deleted, change the previous node’s next pointer to the next
pointer of the node to be deleted.
-
The current node to be deleted will be unlinked with the
list.
8. Display: Traverse and display the data of
all nodes starting from head node.
-
Traverse the linked list(1st operation) and at each node,
display its content
-
This traversing method can also be used to count the number
of nodes in the linked list.