
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Circular Doubly Linked List
Circular Doubly Linked List
A circular linked list is called a circular doubly linked list in which each node has two links connecting it to the previous node and the next node.
In Circular Doubly Linked List two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and the first node also points to last node by previous pointer.

Characteristics of Circular Doubly Linked List
The following are the characteristics of the circular doubly linked list:
- Circular: The main feature is that it is circular in design.
- Doubly Linked: Each node in a circular linked list has two pointers. (i.e., next and previous).
- Header Node: A circular doubly linked list has a header node, which is frequently used to make execution of a certain operation.
In data structure Linked List is a linear collection of data elements. Each element or node of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. In a linked list the entry point is called the head of the list.
Algorithm of Circular Doubly Linked List
The below algorithm is represent the implementation of circular double liked list:
Begin We shall create a class circulardoublylist which have the following functions: nod *create_node(int) = To memory allocated for node dynamically. insert_begin() = To Insert elements at beginning of the list. A) If the list is empty, then insert the node and set next and previous pointer as NULL. B) If the list is not empty, insert the data and set next and previous pointer and update them. insert_end() = To Insert elements at end of the list: A) If the list is empty create a node as circular doubly list. B) Find last node. C) Create node dynamically. D) Start going to be the next of new node. E) Make new node as previous node. F) Make last previous of new node. G) Make new node next of old last. insert_pos() = To insert elements at a specified position of the list: A) Insert the data. B) Enter the position at which element to be inserted. C) If the list is empty insert node at first. D) If list is not empty find node having position and next node. E) Insert the node between them. delete_pos() = To delete elements from specified position of the list: A) If list is empty, then return. B) Enter the position from which node needs to be deleted. C) If list has one node delete it and update next and prev pointers. D) If list has more than one nodes, then delete the node at particular position and update next and prev pointer. search() = To search element in the list: A) If the list is empty, then return. B) Enter the value to be searched. C) Print the position at which element to be found. D) If the element is not found, print not found. update() = To update value at a particular node: A) If the list is empty, then return. B) Enter the position of node to be updated. C) Enter the new value. D) Update the node. display() = To display the list. reverse() = To reverse the list. End
Implementation of Circular Double Linked List
Following is the C++ implementation of the circular doubly linked list to insert the element and display them:
#include<iostream> using namespace std; struct Node { int info; Node * next; Node * prev; }; class CircularDoublyLinkedList { public: Node * start; Node * last; CircularDoublyLinkedList() { start = nullptr; last = nullptr; } Node * createNode(int value) { Node * newNode = new Node(); newNode -> info = value; newNode -> next = newNode -> prev = nullptr; return newNode; } void insertEnd(int value) { Node * newNode = createNode(value); if (!start) { start = last = newNode; start -> next = start -> prev = start; } else { last -> next = newNode; newNode -> prev = last; newNode -> next = start; start -> prev = newNode; last = newNode; } } void display() { if (!start) { cout << "List is empty" << endl; return; } Node * temp = start; do { cout << temp -> info << " <-> "; temp = temp -> next; } while (temp != start); cout << endl; } }; int main() { CircularDoublyLinkedList list; list.insertEnd(10); list.insertEnd(20); list.insertEnd(30); list.insertEnd(40); list.insertEnd(50); // Display the list list.display(); return 0; }
This is the circular doubly linked list:
10 <-> 20 <-> 30 <-> 40 <-> 50 <->
Advertisements