Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by the previous pointer.
Following is the representation of a Circular doubly linked list node in C/C++:Â
C++
static class node {Â Â int data;Â
  // pointer to next node  node next;Â
  // pointer to prev node  node prev;}  // This code is contributed by Yash Agarwal(yashagarwal2852002) |
C
// Structure of the nodestruct node {Â Â Â Â int data;Â
    // Pointer to next node    struct node* next;Â
    // Pointer to previous node    struct node* prev;}; |
C#
public class Node {    public int Data    {        get;        set;    }    public Node Next    {        get;        set;    }    public Node Prev    {        get;        set;    }}Â
public class DoublyLinkedList {    // The Doubly Linked List can be implemented here.    // You can create instances of Node to build your list.} |
Â
Circular Doubly Linked LIst
Insertion in Circular Doubly Linked List:
1. Insertion at the end of the list or in an empty list:
A node(Say N) is inserted with data = 5. So, the previous pointer of N points to N and the next pointer of N also points to N. But now start pointer points to the first node of the list.
Insertion in an empty list
2. List initially contains some nodes, start points to the first node of the List:Â
A node(Say M) is inserted with data = 7, so the previous pointer of M points to the last node, the next pointer of M points to the first node and the last node’s next pointer points to this M node, and first node’s previous pointer points to this M node.
Insertion at the end of list
Below is the implementation of the above operations:
C++
// Function to insert at the endvoid insertEnd(struct Node** start, int value){    // If the list is empty, create a single node    // circular and doubly list    if (*start == NULL) {        struct Node* new_node = new Node;        new_node->data = value;        new_node->next = new_node->prev = new_node;        *start = new_node;        return;    }Â
    // If list is not emptyÂ
    /* Find last node */    Node* last = (*start)->prev;Â
    // Create Node dynamically    struct Node* new_node = new Node;    new_node->data = value;Â
    // Start is going to be next of new_node    new_node->next = *start;Â
    // Make new node previous of start    (*start)->prev = new_node;Â
    // Make last previous of new node    new_node->prev = last;Â
    // Make new node next of old last    last->next = new_node;} |
Java
// Function to insert at the endstatic void insertEnd(int value){Â
    // If the list is empty, create a single    // node circular and doubly list    if (start == null) {        Node new_node = new Node();        new_node.data = value;        new_node.next = new_node.prev = new_node;        start = new_node;        return;    }Â
    // If list is not emptyÂ
    // Find last node    Node last = (start).prev;Â
    // Create Node dynamically    Node new_node = new Node();    new_node.data = value;Â
    // Start is going to be    // next of new_node    new_node.next = start;Â
    // Make new node previous of start    (start).prev = new_node;Â
    // Make last previous of new node    new_node.prev = last;Â
    // Make new node next of old last    last.next = new_node;}Â
// This code is contributed by rutvik_56 |
Python3
# Function to insert at the enddef insertEnd(value):Â Â Â Â global startÂ
    # If the list is empty, create a    # single node circular and doubly list    if (start == None):Â
        new_node = Node(0)        new_node.data = value        new_node.next = new_node.prev = new_node        start = new_node        returnÂ
    # If list is not emptyÂ
    # Find last node */    last = (start).prevÂ
    # Create Node dynamically    new_node = Node(0)    new_node.data = valueÂ
    # Start is going to be next of new_node    new_node.next = startÂ
    # Make new node previous of start    (start).prev = new_nodeÂ
    # Make last previous of new node    new_node.prev = lastÂ
    # Make new node next of old last    last.next = new_nodeÂ
    # This code is contributed by shivanisinghss2110 |
C#
// Function to insert at the endstatic void insertEnd(int value){Â Â Â Â Node new_node;Â
    // If the list is empty, create a single node    // circular and doubly list    if (start == null) {        new_node = new Node();        new_node.data = value;        new_node.next = new_node.prev = new_node;        start = new_node;        return;    }Â
    // If list is not emptyÂ
    /* Find last node */    Node last = (start).prev;Â
    // Create Node dynamically    new_node = new Node();    new_node.data = value;Â
    // Start is going to be next of new_node    new_node.next = start;Â
    // Make new node previous of start    (start).prev = new_node;Â
    // Make last previous of new node    new_node.prev = last;Â
    // Make new node next of old last    last.next = new_node;}Â
// This code is contributed by Pratham76 |
Javascript
// Function to insert at the end function insertEnd(value) {          // If the list is empty, create a single      // node circular and doubly list     if (start == null)     {         var new_node = new Node();         new_node.data = value;         new_node.next = new_node.prev = new_node;         start = new_node;         return;     }       // If list is not empty       // Find last node     var last = (start).prev;       // Create Node dynamically     var new_node = new Node();     new_node.data = value;       // Start is going to be       // next of new_node     new_node.next = start;       // Make new node previous of start     (start).prev = new_node;       // Make last previous of new node     new_node.prev = last;       // Make new node next of old last     last.next = new_node; } Â
Â
// This code contributed by aashish1995 |
3. Insertion at the beginning of the list:Â
To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer points to the first node of the list, T previous pointer points to the last node of the list, last node’s next pointer points to this T node, first node’s previous pointer also points this T node and at last don’t forget to shift ‘Start’ pointer to this T node.
Insertion at the beginning of the list
Below is the implementation of the above operation:
C++
// Function to insert Node at the beginning// of the List,void insertBegin(struct Node** start, int value){    // Pointer points to last Node    struct Node* last = (*start)->prev;Â
    struct Node* new_node = new Node;    new_node->data = value; // Inserting the dataÂ
    // setting up previous and next of new node    new_node->next = *start;    new_node->prev = last;Â
    // Update next and previous pointers of start    // and last.    last->next = (*start)->prev = new_node;Â
    // Update start pointer    *start = new_node;} |
Java
// Function to insert Node at the beginning// of the List,static void insertBegin(int value){    // Pointer points to last Node    Node last = (start).prev;Â
    Node new_node = new Node();    new_node.data = value; // Inserting the dataÂ
    // setting up previous and next of new node    new_node.next = start;    new_node.prev = last;Â
    // Update next and previous pointers of start    // and last.    last.next = (start).prev = new_node;Â
    // Update start pointer    start = new_node;}Â
// this code is contributed by shivanisinghss2110 |
Python3
# Function to insert Node at the beginning# of the List,Â
Â
def insertBegin(value):Â Â Â Â global startÂ
    # Pointer points to last Node    last = (start).prevÂ
    new_node = Node(0)    new_node.data = value # Inserting the dataÂ
    # setting up previous and    # next of new node    new_node.next = start    new_node.prev = lastÂ
    # Update next and previous pointers    # of start and last.    last.next = (start).prev = new_nodeÂ
    # Update start pointer    start = new_nodeÂ
    # This code is contributed by shivanisinghss2110 |
C#
// Function to insert Node at the beginning// of the List,static void insertBegin(int value){Â
    // Pointer points to last Node    Node last = (start).prev;Â
    Node new_node = new Node();    new_node.data = value; // Inserting the dataÂ
    // setting up previous and next of new node    new_node.next = start;    new_node.prev = last;Â
    // Update next and previous pointers of start    // and last.    last.next = (start).prev = new_node;Â
    // Update start pointer    start = new_node;}Â
// This code is contributed by shivanisinghss2110 |
Javascript
// Function to insert Node at the beginning      // of the List,      function insertBegin(value) {        // Pointer points to last Node        var last = start.prev;Â
        var new_node = new Node();        new_node.data = value; // Inserting the dataÂ
        // setting up previous and next of new node        new_node.next = start;        new_node.prev = last;Â
        // Update next and previous pointers of start        // and last.        last.next = start.prev = new_node;Â
        // Update start pointer        start = new_node;      }             // This code is contributed by shivanisinghss2110 |
4. Insertion in between the nodes of the list:Â
To insert a node in between the list, two data values are required one after which new node will be inserted and another is the data of the new node.
Insertion in between other nodes
Below is the implementation of the above operation:
C++
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2void insertAfter(struct Node** start, int value1,                 int value2){    struct Node* new_node = new Node;    new_node->data = value1; // Inserting the dataÂ
    // Find node having value2 and next node of it    struct Node* temp = *start;    while (temp->data != value2)        temp = temp->next;    struct Node* next = temp->next;Â
    // insert new_node between temp and next.    temp->next = new_node;    new_node->prev = temp;    new_node->next = next;    next->prev = new_node;} |
Java
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2static void insertAfter(int value1, int value2){Â Â Â Â Node new_node = new Node();Â Â Â Â new_node.data = value1; // Inserting the dataÂ
    // Find node having value2 and next node of it    Node temp = start;    while (temp.data != value2)        temp = temp.next;    Node next = temp.next;Â
    // insert new_node between temp and next.    temp.next = new_node;    new_node.prev = temp;    new_node.next = next;    next.prev = new_node;}Â
// this code is contributed by shivanisinghss2110 |
Python3
# Function to insert node with value as value1.# The new node is inserted after the node with# with value2Â
Â
def insertAfter(value1, value2):    global start    new_node = Node(0)    new_node.data = value1 # Inserting the dataÂ
    # Find node having value2 and    # next node of it    temp = start    while (temp.data != value2):        temp = temp.next    next = temp.nextÂ
    # insert new_node between temp and next.    temp.next = new_node    new_node.prev = temp    new_node.next = next    next.prev = new_nodeÂ
# this code is contributed by shivanisinghss2110 |
C#
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2static void insertAfter(int value1, int value2){Â Â Â Â Node new_node = new Node();Â Â Â Â new_node.data = value1; // Inserting the dataÂ
    // Find node having value2 and next node of it    Node temp = start;    while (temp.data != value2)        temp = temp.next;    Node next = temp.next;Â
    // insert new_node between temp and next.    temp.next = new_node;    new_node.prev = temp;    new_node.next = next;    next.prev = new_node;}Â
// this code is contributed by shivanisinghss2110 |
Javascript
<script>Â
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2function insertAfter(value1, value2) {    var new_node = new Node();         // Inserting the data    new_node.data = value1; Â
    // Find node having value2 and    // next node of it    var temp = start;    while (temp.data != value2)         temp = temp.next;             var next = temp.next;Â
    // Insert new_node between temp and next.    temp.next = new_node;    new_node.prev = temp;    new_node.next = next;    next.prev = new_node;}       // This code is contributed by shivanisinghss2110Â
</script> |
Following is a complete program that uses all of the above methods to create a circular doubly linked list. Â
C++
// C++ program to illustrate inserting a Node in// a Circular Doubly Linked list in begging, end// and middle#include <bits/stdc++.h>using namespace std;Â
// Structure of a Nodestruct Node {Â Â Â Â int data;Â Â Â Â struct Node* next;Â Â Â Â struct Node* prev;};Â
// Function to insert at the endvoid insertEnd(struct Node** start, int value){    // If the list is empty, create a single node    // circular and doubly list    if (*start == NULL) {        struct Node* new_node = new Node;        new_node->data = value;        new_node->next = new_node->prev = new_node;        *start = new_node;        return;    }Â
    // If list is not emptyÂ
    /* Find last node */    Node* last = (*start)->prev;Â
    // Create Node dynamically    struct Node* new_node = new Node;    new_node->data = value;Â
    // Start is going to be next of new_node    new_node->next = *start;Â
    // Make new node previous of start    (*start)->prev = new_node;Â
    // Make last previous of new node    new_node->prev = last;Â
    // Make new node next of old last    last->next = new_node;}Â
// Function to insert Node at the beginning// of the List,void insertBegin(struct Node** start, int value){    // Pointer points to last Node    struct Node* last = (*start)->prev;Â
    struct Node* new_node = new Node;    new_node->data = value; // Inserting the dataÂ
    // setting up previous and next of new node    new_node->next = *start;    new_node->prev = last;Â
    // Update next and previous pointers of start    // and last.    last->next = (*start)->prev = new_node;Â
    // Update start pointer    *start = new_node;}Â
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2void insertAfter(struct Node** start, int value1,                 int value2){    struct Node* new_node = new Node;    new_node->data = value1; // Inserting the dataÂ
    // Find node having value2 and next node of it    struct Node* temp = *start;    while (temp->data != value2)        temp = temp->next;    struct Node* next = temp->next;Â
    // insert new_node between temp and next.    temp->next = new_node;    new_node->prev = temp;    new_node->next = next;    next->prev = new_node;}Â
void display(struct Node* start){Â Â Â Â struct Node* temp = start;Â
    printf("\nTraversal in forward direction \n");    while (temp->next != start) {        printf("%d ", temp->data);        temp = temp->next;    }    printf("%d ", temp->data);Â
    printf("\nTraversal in reverse direction \n");    Node* last = start->prev;    temp = last;    while (temp->prev != last) {        printf("%d ", temp->data);        temp = temp->prev;    }    printf("%d ", temp->data);}Â
/* Driver program to test above functions*/int main(){Â Â Â Â /* Start with the empty list */Â Â Â Â struct Node* start = NULL;Â
    // Insert 5. So linked list becomes 5->NULL    insertEnd(&start, 5);Â
    // Insert 4 at the beginning. So linked    // list becomes 4->5    insertBegin(&start, 4);Â
    // Insert 7 at the end. So linked list    // becomes 4->5->7    insertEnd(&start, 7);Â
    // Insert 8 at the end. So linked list    // becomes 4->5->7->8    insertEnd(&start, 8);Â
    // Insert 6, after 5. So linked list    // becomes 4->5->6->7->8    insertAfter(&start, 6, 5);Â
    printf("Created circular doubly linked list is: ");    display(start);Â
    return 0;} |
Java
// Java program to illustrate inserting a Node in// a Circular Doubly Linked list in begging, end// and middleimport java.util.*;Â
class GFG {Â
    static Node start;Â
    // Structure of a Node    static class Node {        int data;        Node next;        Node prev;    };Â
    // Function to insert at the end    static void insertEnd(int value)    {        // If the list is empty, create a single node        // circular and doubly list        if (start == null) {            Node new_node = new Node();            new_node.data = value;            new_node.next = new_node.prev = new_node;            start = new_node;            return;        }Â
        // If list is not emptyÂ
        /* Find last node */        Node last = (start).prev;Â
        // Create Node dynamically        Node new_node = new Node();        new_node.data = value;Â
        // Start is going to be next of new_node        new_node.next = start;Â
        // Make new node previous of start        (start).prev = new_node;Â
        // Make last previous of new node        new_node.prev = last;Â
        // Make new node next of old last        last.next = new_node;    }Â
    // Function to insert Node at the beginning    // of the List,    static void insertBegin(int value)    {        // Pointer points to last Node        Node last = (start).prev;Â
        Node new_node = new Node();        new_node.data = value; // Inserting the dataÂ
        // setting up previous and next of new node        new_node.next = start;        new_node.prev = last;Â
        // Update next and previous pointers of start        // and last.        last.next = (start).prev = new_node;Â
        // Update start pointer        start = new_node;    }Â
    // Function to insert node with value as value1.    // The new node is inserted after the node with    // with value2    static void insertAfter(int value1, int value2)    {        Node new_node = new Node();        new_node.data = value1; // Inserting the dataÂ
        // Find node having value2 and next node of it        Node temp = start;        while (temp.data != value2)            temp = temp.next;        Node next = temp.next;Â
        // insert new_node between temp and next.        temp.next = new_node;        new_node.prev = temp;        new_node.next = next;        next.prev = new_node;    }Â
    static void display()    {        Node temp = start;Â
        System.out.printf(            "\nTraversal in forward direction \n");        while (temp.next != start) {            System.out.printf("%d ", temp.data);            temp = temp.next;        }        System.out.printf("%d ", temp.data);Â
        System.out.printf(            "\nTraversal in reverse direction \n");        Node last = start.prev;        temp = last;        while (temp.prev != last) {            System.out.printf("%d ", temp.data);            temp = temp.prev;        }        System.out.printf("%d ", temp.data);    }Â
    /* Driver code*/    public static void main(String[] args)    {        /* Start with the empty list */        Node start = null;Â
        // Insert 5. So linked list becomes 5.null        insertEnd(5);Â
        // Insert 4 at the beginning. So linked        // list becomes 4.5        insertBegin(4);Â
        // Insert 7 at the end. So linked list        // becomes 4.5.7        insertEnd(7);Â
        // Insert 8 at the end. So linked list        // becomes 4.5.7.8        insertEnd(8);Â
        // Insert 6, after 5. So linked list        // becomes 4.5.6.7.8        insertAfter(6, 5);Â
        System.out.printf(            "Created circular doubly linked list is: ");        display();    }}Â
// This code is contributed by Rajput-Ji |
Python3
# Python3 program to illustrate inserting# a Node in a Circular Doubly Linked list# in begging, end and middleÂ
# Structure of a Nodeclass Node:    def __init__(self, data):        self.data = data        self.next = None        self.prev = NoneÂ
# Function to insert at the endÂ
Â
def insertEnd(value):Â Â Â Â global startÂ
    # If the list is empty, create a    # single node circular and doubly list    if (start == None):Â
        new_node = Node(0)        new_node.data = value        new_node.next = new_node.prev = new_node        start = new_node        returnÂ
    # If list is not emptyÂ
    # Find last node */    last = (start).prevÂ
    # Create Node dynamically    new_node = Node(0)    new_node.data = valueÂ
    # Start is going to be next of new_node    new_node.next = startÂ
    # Make new node previous of start    (start).prev = new_nodeÂ
    # Make last previous of new node    new_node.prev = lastÂ
    # Make new node next of old last    last.next = new_nodeÂ
# Function to insert Node at the beginning# of the List,Â
Â
def insertBegin(value):Â Â Â Â global startÂ
    # Pointer points to last Node    last = (start).prevÂ
    new_node = Node(0)    new_node.data = value # Inserting the dataÂ
    # setting up previous and    # next of new node    new_node.next = start    new_node.prev = lastÂ
    # Update next and previous pointers    # of start and last.    last.next = (start).prev = new_nodeÂ
    # Update start pointer    start = new_nodeÂ
# Function to insert node with value as value1.# The new node is inserted after the node with# with value2Â
Â
def insertAfter(value1, value2):    global start    new_node = Node(0)    new_node.data = value1 # Inserting the dataÂ
    # Find node having value2 and    # next node of it    temp = start    while (temp.data != value2):        temp = temp.next    next = temp.nextÂ
    # insert new_node between temp and next.    temp.next = new_node    new_node.prev = temp    new_node.next = next    next.prev = new_nodeÂ
Â
def display():    global start    temp = startÂ
    print("Traversal in forward direction:")    while (temp.next != start):Â
        print(temp.data, end=" ")        temp = temp.nextÂ
    print(temp.data)Â
    print("Traversal in reverse direction:")    last = start.prev    temp = last    while (temp.prev != last):Â
        print(temp.data, end=" ")        temp = temp.prevÂ
    print(temp.data)Â
Â
# Driver Codeif __name__ == '__main__':Â Â Â Â global startÂ
    # Start with the empty list    start = NoneÂ
    # Insert 5. So linked list becomes 5.None    insertEnd(5)Â
    # Insert 4 at the beginning. So linked    # list becomes 4.5    insertBegin(4)Â
    # Insert 7 at the end. So linked list    # becomes 4.5.7    insertEnd(7)Â
    # Insert 8 at the end. So linked list    # becomes 4.5.7.8    insertEnd(8)Â
    # Insert 6, after 5. So linked list    # becomes 4.5.6.7.8    insertAfter(6, 5)Â
    print("Created circular doubly linked list is: ")    display()Â
# This code is contributed by Arnab kundu |
C#
// C# program to illustrate inserting a Node in// a Circular Doubly Linked list in begging, end// and middleusing System;using System.Collections.Generic;Â
class GFG {Â Â Â Â static Node start;Â
    // Structure of a Node    public class Node {        public int data;        public Node next;        public Node prev;    };Â
    // Function to insert at the end    static void insertEnd(int value)    {        Node new_node;Â
        // If the list is empty, create a single node        // circular and doubly list        if (start == null) {            new_node = new Node();            new_node.data = value;            new_node.next = new_node.prev = new_node;            start = new_node;            return;        }Â
        // If list is not emptyÂ
        /* Find last node */        Node last = (start).prev;Â
        // Create Node dynamically        new_node = new Node();        new_node.data = value;Â
        // Start is going to be next of new_node        new_node.next = start;Â
        // Make new node previous of start        (start).prev = new_node;Â
        // Make last previous of new node        new_node.prev = last;Â
        // Make new node next of old last        last.next = new_node;    }Â
    // Function to insert Node at the beginning    // of the List,    static void insertBegin(int value)    {        // Pointer points to last Node        Node last = (start).prev;Â
        Node new_node = new Node();        new_node.data = value; // Inserting the dataÂ
        // setting up previous and next of new node        new_node.next = start;        new_node.prev = last;Â
        // Update next and previous pointers of start        // and last.        last.next = (start).prev = new_node;Â
        // Update start pointer        start = new_node;    }Â
    // Function to insert node with value as value1.    // The new node is inserted after the node with    // with value2    static void insertAfter(int value1, int value2)    {        Node new_node = new Node();        new_node.data = value1; // Inserting the dataÂ
        // Find node having value2 and next node of it        Node temp = start;        while (temp.data != value2)            temp = temp.next;        Node next = temp.next;Â
        // insert new_node between temp and next.        temp.next = new_node;        new_node.prev = temp;        new_node.next = next;        next.prev = new_node;    }Â
    static void display()    {        Node temp = start;Â
        Console.Write(            "\nTraversal in forward direction \n");        while (temp.next != start) {            Console.Write("{0} ", temp.data);            temp = temp.next;        }        Console.Write("{0} ", temp.data);Â
        Console.Write(            "\nTraversal in reverse direction \n");        Node last = start.prev;        temp = last;        while (temp.prev != last) {            Console.Write("{0} ", temp.data);            temp = temp.prev;        }        Console.Write("{0} ", temp.data);    }Â
    /* Driver code*/    public static void Main(String[] args)    {        /* Start with the empty list */        Node start = null;Â
        // Insert 5. So linked list becomes 5.null        insertEnd(5);Â
        // Insert 4 at the beginning. So linked        // list becomes 4.5        insertBegin(4);Â
        // Insert 7 at the end. So linked list        // becomes 4.5.7        insertEnd(7);Â
        // Insert 8 at the end. So linked list        // becomes 4.5.7.8        insertEnd(8);Â
        // Insert 6, after 5. So linked list        // becomes 4.5.6.7.8        insertAfter(6, 5);Â
        Console.Write(            "Created circular doubly linked list is: ");        display();    }}Â
// This code is contributed by Rajput-Ji |
Javascript
// JavaScript program to illustrate inserting a Node in// a Circular Doubly Linked list in begging, end// and middlevar start = null;Â
// Structure of a Nodeclass Node {Â Â constructor() {Â Â Â Â this.data = 0;Â Â Â Â this.next = null;Â Â Â Â this.prev = null;Â Â }}Â
// Function to insert at the endfunction insertEnd(value) {Â Â var new_node;Â
  // If the list is empty, create a single node  // circular and doubly list  if (start == null) {    new_node = new Node();    new_node.data = value;    new_node.next = new_node.prev = new_node;    start = new_node;    return;  }Â
  // If list is not emptyÂ
  /* Find last node */  var last = start.prev;Â
  // Create Node dynamically  new_node = new Node();  new_node.data = value;Â
  // Start is going to be next of new_node  new_node.next = start;Â
  // Make new node previous of start  start.prev = new_node;Â
  // Make last previous of new node  new_node.prev = last;Â
  // Make new node next of old last  last.next = new_node;}Â
// Function to insert Node at the beginning// of the List,function insertBegin(value) {  // Pointer points to last Node  var last = start.prev;Â
  var new_node = new Node();  new_node.data = value; // Inserting the dataÂ
  // setting up previous and next of new node  new_node.next = start;  new_node.prev = last;Â
  // Update next and previous pointers of start  // and last.  last.next = start.prev = new_node;Â
  // Update start pointer  start = new_node;}Â
// Function to insert node with value as value1.// The new node is inserted after the node with// with value2function insertAfter(value1, value2) {Â Â var new_node = new Node();Â Â new_node.data = value1; // Inserting the dataÂ
  // Find node having value2 and next node of it  var temp = start;  while (temp.data != value2) temp = temp.next;  var next = temp.next;Â
  // insert new_node between temp and next.  temp.next = new_node;  new_node.prev = temp;  new_node.next = next;  next.prev = new_node;}Â
function display() {Â Â var temp = start;Â
  document.write("<br>Traversal in forward direction <br>");  while (temp.next != start) {    document.write(temp.data + " ");    temp = temp.next;  }  document.write(temp.data);Â
  document.write("<br>Traversal in reverse direction <br>");  var last = start.prev;  temp = last;  while (temp.prev != last) {    document.write(temp.data + " ");    temp = temp.prev;  }  document.write(temp.data);}Â
/* Driver code*//* Start with the empty list */var start = null;Â
// Insert 5. So linked list becomes 5.nullinsertEnd(5);Â
// Insert 4 at the beginning. So linked// list becomes 4.5insertBegin(4);Â
// Insert 7 at the end. So linked list// becomes 4.5.7insertEnd(7);Â
// Insert 8 at the end. So linked list// becomes 4.5.7.8insertEnd(8);Â
// Insert 6, after 5. So linked list// becomes 4.5.6.7.8insertAfter(6, 5);Â
document.write("Created circular doubly linked list is: ");display(); |
Created circular doubly linked list is: Traversal in forward direction 4 5 6 7 8 Traversal in reverse direction 8 7 6 5 4
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Advantages of circular doubly linked list:Â
- The list can be traversed from both directions i.e. from head to tail or from tail to head.
- Jumping from head to tail or from tail to head is done in constant time O(1).
- Circular Doubly Linked Lists are used for the implementation of advanced data structures like the Fibonacci Heap.
Disadvantages of circular doubly linked list:Â
- It takes slightly extra memory in each node to accommodate the previous pointer.
- Lots of pointers are involved while implementing or doing operations on a list. So, pointers should be handled carefully otherwise data of the list may get lost.
Applications of Circular doubly linked list:
- Managing song playlists in media player applications.
- Managing shopping carts in online shopping.
This article is contributed by Akash Gupta. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
