Circular Linked List Implementation
In a circular Singly 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.
As we know that singly linked list and doubly linked list are already present in there in data structure and algorithm then what is the need of circular linked list?
When we want to access the things in a loop repeatedly. Circular Linked List is ideal so that, whenever we reach the last node we can restart operations again by directly reaching the first node from the last node itself.
There are basically two types of circular linked list:
- Circular Singly Linked List
- Circular Doubly Linked List
Following is a Circular Singly Linked List representation.
Algorithm
Node creation :
- Singly circular linked list
- Doubly circular linked list
struct Node
{
int data;
struct Node *next;
}
struct node
{
struct node *prev, *next;
int data;
}
Insertion :
- Insertion at beginning - Adding a node into circular singly linked list at the beginning.
- Insertion at the end - Adding a node into circular singly linked list at the end.
Deletion & Traversal:
- Deletion at beginning - Removing the node from circular singly linked list at the beginning.
- Deletion at the end - Removing the node from circular singly linked list at the end.
- Searching - Compare each element of the node with the given item and return the location at which the item is present in the list otherwise return null.
- Traversing - Visiting each element of the list at least once in order to perform some specific operation.