14 - Data Structure - Circular Linked List
14 - Data Structure - Circular Linked List
List
In singly linked list, the next pointer of the last node points to the first node.
In doubly linked list, the next pointer of the last node points to the first node
and the previous pointer of the first node points to the last node making the
circular in both directions.
The last link's next points to the first link of the list in both cases of
singly as well as doubly linked list.
The first link's previous points to the last of the list in case of doubly
linked list.
Basic Operations
Insertion Operation
The insertion operation of a circular linked list only inserts the element at the
start of the list. This differs from the usual singly and doubly linked lists as
there is no particular starting and ending points in this list. The insertion is
done either at the start or after a particular node (or a given position) in the
list.
Algorithm
1. START
3. If the list is empty, add the node and point the head to this node
4. If the list is not empty, link the existing head as the next node to the new
node.
6. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
};
bool isEmpty(){
//create a link
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
link->next = head;
head = link;
void printList(){
printf("\n[ ");
//start from the beginning
if(head != NULL) {
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
printf(" ]");
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
Output
Deletion Operation
The Deletion operation in a Circular linked list removes a certain node from the
list. The deletion operation in this type of lists can be done at the beginning, or
a given position, or at the ending.
Algorithm
1. START
3. If the list is not empty, we traverse the list using a current pointer that is set
to the head pointer and create another pointer previous that points to the last
node.
4. Suppose the list has only one node, the node is deleted by setting the head
pointer to NULL.
5. If the list has more than one node and the first node is to be deleted, the
head is set to the next node and the previous is linked to the new head.
6. If the node to be deleted is the last node, link the preceding node of the last
node to head node.
7. If the node is neither first nor last, remove the node by linking its preceding
node to its succeeding node.
8. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
};
bool isEmpty(){
//create a link
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
link->next = head;
head = link;
}
if(head->next == head) {
head = NULL;
return tempLink;
head = head->next;
return tempLink;
}
//display the list
void printList(){
if(head != NULL) {
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
//print list
printList();
deleteFirst();
printList();
Output
List after deleting the first item: (5,40) (4,1) (3,30) (2,20)
The Display List operation visits every node in the list and prints them all in
the output.
Algorithm
1. START
2. Walk through all the nodes of the list and print them
3. END
Example
Following are the implementations of this operation in various programming
languages −
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
};
bool isEmpty(){
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
link->next = head;
head = link;
printf("\n[ ");
if(head != NULL) {
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
printf(" ]");
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
//print list
printList();
Output