C++ Data Structures - Circular Doubly Linked List Other Related Topics
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ - Circular Doubly Linked List



A circular doubly linked list is a linear data structure, in which the elements are stored in the form of a node. Each node contains three sub-elements. A data part that stores the value of the element, the previous part that stores the pointer to the previous node, and the next part that stores the pointer to the next node as shown in the below image:

Linked List Node

The first node also known as HEAD is always used as a reference to traverse the list. Last element contains link to the first element as next and the first element contains link of the last element as previous. A circular doubly linked can be visualized as a chain of nodes, where every node points to previous and next node.

Linked List

Implementation of Circular Doubly Linked List

Representation:

In C++, doubly linked list can be created using a class and a Node using structures. The LinkedList class contains Node as class member.

//node structure
struct Node {
    int data;
    Node* next;
    Node* prev;
};

class LinkedList {
  public:
    Node* head;
  public:
    //constructor to create an empty LinkedList
    LinkedList(){
      head = NULL;
    }
};

Create a Circular Doubly Linked List

Let us create a simple circular doubly linked list which contains three data nodes.

#include <iostream>
using namespace std;

//node structure
struct Node {
    int data;
    Node* next;
    Node* prev;
};

class LinkedList {
  public:
    Node* head;
  public:
    //constructor to create an empty LinkedList
    LinkedList(){
      head = NULL;
    } 
};

// test the code  
int main() {
  //create an empty LinkedList 
  LinkedList MyList;

  //Add first node.
  Node* first = new Node();
  first->data = 10;
  first->next = NULL;
  first->prev = NULL;
  //linking with head node
  MyList.head = first;
  //linking next of the node with head
  first->next = MyList.head;
  //linking prev of the head 
  MyList.head->prev = first;        

  //Add second node.
  Node* second = new Node();
  second->data = 20;
  second->next = NULL;
  //linking with first node
  second->prev = first;  
  first->next = second;
  //linking next of the node with head
  second->next = MyList.head;
  //linking prev of the head 
  MyList.head->prev = second;   

  //Add third node.
  Node* third = new Node();
  third->data = 30;
  third->next = NULL;
  //linking with second node
  third->prev = second;
  second->next = third;
  //linking next of the node with head
  third->next = MyList.head; 
  //linking prev of the head 
  MyList.head->prev = third; 

  return 0;
}

Traverse a Circular Doubly Linked List

A circular doubly linked list can be traversed from any node of the list using a temp node. Keep on moving the temp node to the next one and displaying its content. Stop the traversal, after reaching the starting node.

#include <iostream>
using namespace std;

//node structure
struct Node {
    int data;
    Node* next;
    Node* prev;
};

class LinkedList {
  public:
    Node* head;
  public:
    //constructor to create an empty LinkedList
    LinkedList(){
      head = NULL;
    }

    //display the content of the list
    void PrintList() {
      Node* temp = head;
      if(temp != NULL) {
        cout<<"The list contains: ";
        while(true) {
          cout<<temp->data<<" ";
          temp = temp->next;
          if(temp == head) 
            break;
        }
        cout<<endl;
      } else {
        cout<<"The list is empty.\n";
      }
    }     
};

// test the code  
int main() {
  //create an empty LinkedList
  LinkedList MyList;

  //Add first node.
  Node* first = new Node();
  first->data = 10;
  first->next = NULL;
  first->prev = NULL;
  //linking with head node
  MyList.head = first;
  //linking next of the node with head
  first->next = MyList.head;
  //linking prev of the head 
  MyList.head->prev = first;        

  //Add second node.
  Node* second = new Node();
  second->data = 20;
  second->next = NULL;
  //linking with first node
  second->prev = first;  
  first->next = second;
  //linking next of the node with head
  second->next = MyList.head;
  //linking prev of the head 
  MyList.head->prev = second;   

  //Add third node.
  Node* third = new Node();
  third->data = 30;
  third->next = NULL;
  //linking with second node
  third->prev = second;
  second->next = third;
  //linking next of the node with head
  third->next = MyList.head; 
  //linking prev of the head 
  MyList.head->prev = third; 

  //print the content of list
  MyList.PrintList();
  return 0; 
}

The above code will give the following output:

The list contains: 10 20 30

5