![]() |
VOOZH | about |
A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead of NULL creating a circular chain of nodes. In this article, we will learn how to implement a circular linked list in C++.
In C++, to create a , we need to define a class name Node that will consist of the data members for a node of the list. After that, we can create a CircularLinkedList class to encapsulate the data members and functions for the circular linked list.
To represent a node of a circular linked list we will declare a classNode that will consist of two data members data and next where:
template <typename T>
class Node {
public:
T data;
Node<T>* next;
};where,
Note: Here we have used the templates to keep the circular linked list generic, users can store any type of data they want to store in the circular linked list.
The following diagram represents the structure of a circular linked list:
Following are some of the basic operations that are required to work with circular linked list in C++:
Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
Insert | insert At First | Adds a new node at the beginning of the list. | O(N) | O(1) |
InsertAt | Add new node at the specified position. | O(N) | O(1) | |
| insert At End | Adds a new node at the end of the list and points it's next pointer to the head. | O(N) | O(1) | |
Delete | delete From First | Removes the first node and updates the current head of the list. | O(N) | O(1) |
Delete | Deletes the node with given key value. | O(N) | O(1) | |
| delete From End | Removes the last node and points the next pointer of the second last node to the head of the list. | O(N) | O(1) | |
| Display | Iterates through the circular linked list and prints the data present in each node of the circular linked list. | O(N) | O(1) |
Note: Here N represents the number of nodes in the circular linked list.
Following is the algorithm for inserting a node at the start of a circular linked list:
- Create a new Node.
- Confirm whether the circular linked list contains nodes or is empty
- If the list is empty:
- Update the next pointer of the new node to point to itself.
- Set the new node as the head.
- If the list is not empty:
- Set the next pointer of the new node to head.
- Iterate through the list to reach the last node.
- Update the next pointer of the last node to the new node that you have added.
- Update the new node as the head of the list.
Following is the algorithm for inserting a node at the end of the circular linked list:
- Create a new Node.
- Confirm whether the circular linked list contains nodes or is empty
- If the list is empty:
- Update the next pointer of the new node to point to itself.
- Set the new node as the head.
- If the list is not empty:
- Iterate through the list to reach the last node.
- Update the next pointer of the last node to the new node that you have added.
- Update the next pointer of new node to the head of the list.
Following is the algorithm for inserting a node at a given position in the circular linked list:
- Create a new node with the given data.
- Check if the given position is less than or equal to 1 or if the list is empty.
- If true, call insertAtFirst with the given data.
- If the list is not empty:
- Initialize a pointer temp to point to the head of the list.
- Iterate through the list to reach the node just before the given position or until the end of the list:
- For i from 1 to position - 2 (inclusive), update temp to temp->next.
- Update the next pointer of the new node to temp->next.
- Update the next pointer of temp to point to the new node.
Following is the algorithm for deleting the first node of a the circular linked list:
- Check if the head pointer of the list is NULL or not. If NULL,return.
- If the list contains only a single node, then delete the head node and update the Head pointer to NULL.
- If the list has multiple nodes:
- Store the head of the list in a temporary pointer named temp.
- Iterate through the list to reach the last node.
- Update the next pointer of the last node to the next node of the head node.
- Update the head to the next node.
- Delete the temp node.
Following is the algorithm for deleting the last node of a the circular linked list:
- Check if the head pointer of the list is NULL or not. If NULL,return.
- If the list contains only a single node, then delete the head node and update the Head pointer to NULL.
- If the list has multiple nodes:
- Traverse to the node just before the last node in the list.
- Store the last node of the list in a temporary pointer named temp.
- Update the next pointer of the second last node to the head of the list.
- Delete the last node represented by temp pointer.
Following is the algorithm for deleting a node with a given key in the circular linked list:
- Check if the head pointer of the list is NULL. If NULL, return.
- Check if the head node contains the key:
- If true, call deleteFromFirst and return.
- If the list has multiple nodes:
- Initialize a pointer temp to point to the head of the list.
- Initialize a pointer prev to NULL.
- Iterate through the list to find the node with the given key:
- Update prev to temp.
- Update temp to temp->next.
- Repeat until temp is equal to head or temp->data equals the key.
- If the key is found (i.e., temp->data equals the key):
- Update the next pointer of prev to temp->next.
- Delete the temp node.
Following is the algorithm for printing the nodes of a circular linked list:
- Check if the list is empty or not. If empty return.
- Create a temporary pointer temp and point it to the head of the list.
- Do:
- Print the data of the temporary node.
- Move temp to temp->next.
- While:
- temporary pointer is not equal to head.
The below program demonstrates how we can implement a circular linked list in C++:
Output
Circular linked list after inserting node at the beginning:
10 -> HEAD
Circular linked list after inserting 20,30,40,50 at the end:
10 -> 20 -> 30 -> 40 -> 50 -> HEAD
Circular linked list after deleting the first node:
20 -> 30 -> 40 -> 50 -> HEAD
Circular linked list after deleting the last node :
20 -> 30 -> 40 -> HEADFollowing are some of the common applications of circular linked list in our daily lives:
You can also go through the following articles to improve your understanding about the linked lists: