Develop a program on Circular Double Linked List and give a brief
overview
A circular doubly linked list is defined as a circular linked list in which
each node has two links connecting it to the previous node and the next
node.
Characteristics of Circular Doubly Linked List :
A circular doubly linked list has the following properties:
Circular: A circular doubly linked list’s main feature is that it is circular in design.
Doubly Linked: Each node in a circular doubly linked list has two pointers – one
pointing to the node before it and the other pointing to the node after it.
Header Node: At the start of circular doubly linked lists, a header node or sentinel
node is frequently used. This node is used to make the execution of certain operations
on the list simpler even though it is not a component of the list’s actual contents.
Applications of Circular Doubly Linked List :
Circular doubly linked lists are used in a variety of applications, some of which include:
Implementation of Circular Data Structures: Circular doubly linked lists are
extremely helpful in the construction of circular data structures like circular queues and
circular buffers, which are both circular in nature.
Implementing Undo-Redo Operations: Text editors and other software programs can
use circular doubly linked lists to implement undo-redo operations.
Music Player Playlist: Playlists in music players are frequently implemented using
circular doubly linked lists. Each song is kept as a node in the list in this scenario, and
the list can be circled to play the songs in the order they are listed.
Cache Memory Management: To maintain track of the most recently used cache
blocks, circular doubly linked lists are employed in cache memory management.
Operation on CDLL
Insertion in Doubly Circular Linked List
Insertion at Specific Position in a Circular Doubly Linked List
Search an Element in Doubly Circular Linked List
Deletion in Doubly Circular Linked List
Reverse a doubly circular linked list .
Circular Double Linked List Structure Definition:
struct dll {
int data;
struct dll* right;
struct dll* left;
};
Creation of Circular double linked list:
void create(int n) {
int i;
node* newnode, * temp;
for (i = 0; i < n; i++)
{
newnode = getnode();
if (head == NULL)
{
head = newnode;
head->left = head;
head->right = head;
}
else {
temp = head;
while (temp->right != head)
temp = temp->right;
temp->right = newnode;
newnode->left = temp;
newnode->right = head;
head->left = newnode;
}
}
}
getnode function:
node* getnode()
{
node* newnode;
newnode = (node*)malloc(sizeof(node));
printf("Enter data: ");
scanf("%d", & newnode ->data);
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
Display function
void display()
{
node* temp;
if (head == NULL)
{
printf("List is empty.\n");
return;
}
temp = head;
do
{
printf("%d->", temp->data);
temp = temp->right;
}
while (temp != head);
printf("\n");
}
The Three cases of Insertion in Circular Double Linked List:
1. Insertion at beginning
2. Insertion at mid
3. Insertion at end
1. Insert at beginning:
void insbeg()
{
node* newnode = getnode();
if (head == NULL)
{
head = newnode;
head->left = head; // Circular
head->right = head; // Circular
}
else
{
newnode->right = head;
newnode->left = head->left;
head->left->right = newnode;
head->left = newnode;
head = newnode;
}
}
2. Insertion at mid
void insmid() {
int pos, ctr = 1;
printf("Enter the position\n");
scanf("%d", &pos);
node* newnode, * temp, * prev;
newnode = getnode();
if (head == NULL)
{
head = newnode;
head->left = head;
head->right = head;
}
else
{
temp = head;
while (ctr < pos)
{
prev = temp;
temp = temp->right;
ctr++;
}
prev->right = newnode;
newnode->left = prev;
newnode->right = temp;
temp->left = newnode;
}
}
3. Insertion at end:
void insend()
{
node* newnode, * temp;
newnode = getnode();
if (head == NULL)
{
head = newnode;
head->left = head;
head->right = head;
}
else
{
temp = head->left;
newnode->left = temp;
newnode->right = head;
temp->right = newnode;
head->left = newnode;
}
}
The Three Cases of Deletion:
1. Deletion at beginning
2. Deletion at end
3. Deletion at mid
1. Deletion at beginning:
void delbeg()
{
if (head == NULL)
{
printf("No nodes to delete.\n");
return;
}
node* temp = head;
head = head->right;
head->left = temp ->left;
temp->left->right = head;
if (temp == temp->right)
{
free(temp);
head = NULL;
}
else
{
free(temp);
}
}
2. Deletion at mid:
void delmid()
{
int pos, ctr = 1;
printf("Enter position: ");
scanf("%d", &pos);
if (head == NULL)
{
printf("No nodes to delete.\n");
return;
}
node* temp, * prev;
temp = head;
while (ctr < pos)
{
prev = temp;
temp = temp->right;
ctr++;
}
prev->right = temp->right;
temp->right->left = prev;
if (temp == head)
{
head = temp->right;
}
free(temp);
}
3. Deletion at end:
void delend()
{
if (head == NULL)
{
printf("No nodes to delete.\n");
return;
}
node* temp = head->left;
temp->left->right = head;
head->left = temp->left;
free(temp);
}
rdisplay function:
void rdisplay()
{
node* temp;
if (head == NULL)
{
printf("List is empty.\n");
return;
}
temp = head->left;
do {
printf("%d->", temp->data);
temp = temp->left;
}
while (temp != head->left);
printf("\n") }
PROGRAM FOR CIRCULAR DOUBLE LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct LinkedList {
struct Node* head;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode) {
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
struct LinkedList* initializeList() {
struct LinkedList* list = (struct LinkedList*)malloc(sizeof(struct LinkedList));
if (list) {
list->head = NULL;
return list;
void insertEnd(struct LinkedList* list, int data) {
struct Node* newNode = createNode(data);
if (!newNode) {
printf("Memory allocation failed.\n");
return;
if (list->head == NULL) {
list->head = newNode;
newNode->next = newNode;
newNode->prev = newNode;
} else {
struct Node* last = list->head->prev;
newNode->next = list->head;
newNode->prev = last;
last->next = newNode;
list->head->prev = newNode;
void deleteNode(struct LinkedList* list, int data) {
if (list->head == NULL) {
printf("List is empty. Deletion failed.\n");
return;
struct Node* current = list->head;
struct Node* toDelete = NULL;
int found = 0;
do {
if (current->data == data) {
toDelete = current;
found = 1;
break;
current = current->next;
} while (current != list->head);
if (found) {
if (toDelete == list->head) {
list->head = list->head->next;
toDelete->prev->next = toDelete->next;
toDelete->next->prev = toDelete->prev;
free(toDelete);
printf("Node with data %d deleted from the list.\n", data);
} else {
printf("Node with data %d not found in the list.\n", data);
void displayList(struct LinkedList* list) {
if (list->head == NULL) {
printf("List is empty.\n");
return;
struct Node* current = list->head;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != list->head);
printf(" (Circular)\n");
}
void freeList(struct LinkedList* list) {
if (list == NULL) {
return;
if (list->head == NULL) {
free(list);
return;
struct Node* current = list->head;
struct Node* temp;
do {
temp = current;
current = current->next;
free(temp);
} while (current != list->head);
free(list);
int main() {
struct LinkedList* myList = initializeList();
insertEnd(myList, 1);
insertEnd(myList, 2);
insertEnd(myList, 3);
insertEnd(myList, 4);
printf("Circular Doubly Linked List: ");
displayList(myList);
deleteNode(myList, 2);
printf("Updated Circular Doubly Linked List: ");
displayList(myList);
freeList(myList);
return 0;