Introduction to Circular Linked List
Circular linked list data structure is formed to store the collection of elements, by pointing the location of the start element to next of the last element, which is known to form a circular linked list. Most operations of the operating system are required to be performed repeatedly thus needs to be maintained in a queue where it can wait in case any of the required resources are currently acquired by other ongoing processes. Instead of maintaining FIRST and the LAST pointer only one pointer needs to be maintained HEAD to detect the visited node.
Types of Circular Linked List in Data Structure
Circular Linked list is a linked list with no end thus used to perform various continuous operations. With this one HEAD pointer of Node, a type is maintained to identify if all the nodes in the list have been visited. A circular linked list can be easily made by allocating the address of the first node to the next of the last node. Below is the pseudo-code for its nodes.
There are 2 Types of Circular Linked Lists:
- Singly Circular Linked List
- Doubly Circular Linked List
1. Singly Circular Linked List
The type of circular linked list whose every node has a pointer to its next node only is known as a Singly circular linked list.
struct Node* next;
Here next is the pointer that points to the next node in the list.
2. Doubly Circular Linked List
The type of circular linked list whose every node contains the pointer that points to its next as well as the next node in the list. And since it is a circular list last node contains the address of the first node in its next pointer and the first node contains the address of the last node in the previous pointer.
struct Node* next;
struct Node* prev;
Here prev is a pointer that points to the previous node in the linked list and next pointer points to the next node in the list. Such type of linked list in implementation of various other complex data structures.
Operations of Circular Linked List in Data Structure
Operations that can be performed on a circular linked list:
- Create a node T.
- Insert data to be inserted such as, T[data] =data and T[next] = HEAD[next].
- IF HEAD =NULL, i.e empty list, Set HEAD =T and exit.
- Else if LOC = length of A ie. Insertion of the last node, then traverse till last of list A using ptr and then T->next = ptr->next, ptr->next =T.
- Else if LOC =1, in the beginning, T->next =HEAD and HEAD=T.
- Else if traverse to the LOC and T->next = A[LOC] ->next A[LOC] -> next =T.
This algorithm takes a circular linked list A and HEAD pointer as well as n as a data to be inserted and LOC points to the location where n needs to be inserted. Here we assume, that LOC has been calculated by traversing the whole list once and maintaining a counter LOC that is updated till we find the node after which we need to insert a new node. Then a new node T is created. The first list is checked if it is null then, HEAD is updated and it indicates there is only one element in the list after insertion. Otherwise, relevant pointers are updated to insert the node at the beginning or end or in between.
- Check if list A is empty the return -1 [UnderFlow].
- Otherwise call traverse the list to location LOC of a node with Node[data]=n.
- If LOC =-1, return -1 and exit.
- Else If LOC == 1 .ie first node:- Set Head =NULL and exit.
- Else A[LOC -1]->next = A[LOC]->next.
This algorithm checks first if the given list is empty then indicates and Underflow situation otherwise checks at which location deletion needs to be performed and update the pointer accordingly.
- take a ptr =HEAD
- Repeat Steps 3 and 4 till ptr->next !=HEAD
This algorithm is used to traverse the whole circular linked list and perform a print operation for each of the nodes using a pointer ptr that points to the current node of the list. The condition ptr->next !=HEAD is used to observe if any node is not traversed twice.
Benefits of Circular Linked List in Data Structure
- One can start traversal of the complete linked list from any of the nodes since all nodes are lined and there is no endpoint. While traversal we just need to observe not to traverse one node twice.
- Such data structure is most often used for implementing a queue, and since it is a circular list only one pointer needs to be maintained i.e HEAD to store the last inserted node that marks as REAR and FRONT can easily be found.
- Doubly Circular linked lists can also be used to implement various complex data structures such as Fibonacci Heap.
- It is also used by OS to maintain the execution of the various process to run the list continuously. A continuous queue is maintained running with a continuous loop, where each process enters once the CPU is busy and not available. Such a queue is also known as the waiting queue. Various other queues such as priority queue or resources queue is also maintained using this data structure.
- Such data structure can also be used in case of multiplayer games where all the players are entered in a continuous circular linked list and pointer gets incremented once every player’s chance is over.
A circular linked list is a type of data structure where a loop is maintained used by storing the location of the first node of the list in the last node of the linked list thus named as circular. Such a list helps to build a continuous loop that can be used in various games or complex data structures such as the Fibonacci series.
This is a guide to Circular Linked List in Data Structure. Here we discuss operations of Circular Linked List in Data Structure and its types along with its Code Implementation. You can also go through our other suggested articles to learn more –