Convert an Array to a Circular Doubly Linked List - GeeksforGeeks

09 Jul, 2021
Prerequisite: Doubly Linked list, Circular Linked List, Circular Doubly Linked ListGiven an array of N elements. The task is to write a program to convert the array into a circular doubly linked list. 
 
The idea is to start traversing the array and for every array element create a new list node and assign the prev and next pointers of this node accordingly.  
Create a pointer start to point to the starting of the list which will initially point to NULL(Empty list).
For the first element of the array, create a new node and put that node’s prev and next pointers to point to start to maintain the circular fashion of the list.
For the rest of the array elements, insert those elements to the end of the created circular doubly linked list.
Below is the implementation of above idea:  
C++
Java
Python3
C#
Javascript
// CPP program to convert array to// circular doubly linked list #include<bits/stdc++.h>using namespace std; // Doubly linked list nodestruct node{    int data;    struct node *next;    struct node *prev;}; // Utility function to create a node in memorystruct node* getNode(){    return ((struct node *)malloc(sizeof(struct node)));} // Function to display the listint displayList(struct node *temp){    struct node *t = temp;    if(temp == NULL)        return 0;    else    {          cout<<"The list is: ";                 while(temp->next != t)        {            cout<<temp->data<<" ";            temp = temp->next;        }                 cout<<temp->data;                 return 1;    }} // Function to convert array into listvoid createList(int arr[], int n, struct node **start){    // Declare newNode and temporary pointer    struct node *newNode,*temp;    int i;         // Iterate the loop until array length    for(i=0;i<n;i++)    {        // Create new node        newNode = getNode();                 // Assign the array data        newNode->data = arr[i];                 // If it is first element        // Put that node prev and next as start        // as it is circular        if(i==0)        {            *start = newNode;            newNode->prev = *start;            newNode->next = *start;        }        else        {              // Find the last node            temp = (*start)->prev;                         // Add the last node to make them            // in circular fashion            temp->next = newNode;            newNode->next = *start;            newNode->prev = temp;            temp = *start;            temp->prev = newNode;        }    }} // Driver Codeint main(){    // Array to be converted    int arr[] = {1,2,3,4,5};    int n = sizeof(arr) / sizeof(arr[0]);         // Start Pointer    struct node *start = NULL;         // Create the List    createList(arr, n, &start);         // Display the list    displayList(start);         return 0;}
// Java program to convert array to// circular doubly linked listclass GFG{     // Doubly linked list nodestatic class node{    int data;    node next;    node prev;}; // Utility function to create a node in memorystatic node getNode(){    return new node();} // Function to display the liststatic int displayList( node temp){    node t = temp;    if(temp == null)        return 0;    else    {        System.out.print("The list is: ");                 while(temp.next != t)        {            System.out.print(temp.data+" ");            temp = temp.next;        }                 System.out.print(temp.data);                 return 1;    }} // Function to convert array into liststatic node createList(int arr[], int n, node start){    // Declare newNode and temporary pointer    node newNode,temp;    int i;         // Iterate the loop until array length    for(i = 0; i < n; i++)    {        // Create new node        newNode = getNode();                 // Assign the array data        newNode.data = arr[i];                 // If it is first element        // Put that node prev and next as start        // as it is circular        if(i == 0)        {            start = newNode;            newNode.prev = start;            newNode.next = start;        }        else        {            // Find the last node            temp = (start).prev;                         // Add the last node to make them            // in circular fashion            temp.next = newNode;            newNode.next = start;            newNode.prev = temp;            temp = start;            temp.prev = newNode;        }    }    return start;} // Driver Codepublic static void main(String args[]){    // Array to be converted    int arr[] = {1,2,3,4,5};    int n = arr.length;         // Start Pointer    node start = null;         // Create the List    start = createList(arr, n, start);         // Display the list    displayList(start);}} // This code is contributed by Arnab Kundu
# Python3 program to convert array to# circular doubly linked list # Node of the doubly linked listclass Node:         def __init__(self, data):        self.data = data        self.prev = None        self.next = None # Utility function to create a node in memorydef getNode():     return (Node(0)) # Function to display the listdef displayList(temp):     t = temp    if(temp == None):        return 0    else:                 print("The list is: ", end = " ")                 while(temp.next != t):                     print(temp.data, end = " ")            temp = temp.next                 print(temp.data)                 return 1     # Function to convert array into listdef createList(arr, n, start):     # Declare newNode and temporary pointer    newNode = None    temp = None    i = 0         # Iterate the loop until array length    while(i < n):             # Create new node        newNode = getNode()                 # Assign the array data        newNode.data = arr[i]                 # If it is first element        # Put that node prev and next as start        # as it is circular        if(i == 0):                     start = newNode            newNode.prev = start            newNode.next = start                 else:                         # Find the last node            temp = (start).prev                         # Add the last node to make them            # in circular fashion            temp.next = newNode            newNode.next = start            newNode.prev = temp            temp = start            temp.prev = newNode        i = i + 1    return start # Driver Codeif __name__ == "__main__":     # Array to be converted    arr = [1, 2, 3, 4, 5]    n = len(arr)         # Start Pointer    start = None         # Create the List    start = createList(arr, n, start)         # Display the list    displayList(start)     # This code is contributed by Arnab Kundu
// C# program to convert array to// circular doubly linked listusing System; class GFG{     // Doubly linked list nodepublic class node{    public int data;    public node next;    public node prev;}; // Utility function to create a node in memorystatic node getNode(){    return new node();} // Function to display the liststatic int displayList( node temp){    node t = temp;    if(temp == null)        return 0;    else    {        Console.Write("The list is: ");                 while(temp.next != t)        {            Console.Write(temp.data + " ");            temp = temp.next;        }                 Console.Write(temp.data);                 return 1;    }} // Function to convert array into liststatic node createList(int []arr,                        int n, node start){    // Declare newNode and temporary pointer    node newNode,temp;    int i;         // Iterate the loop until array length    for(i = 0; i < n; i++)    {        // Create new node        newNode = getNode();                 // Assign the array data        newNode.data = arr[i];                 // If it is first element        // Put that node prev and next as start        // as it is circular        if(i == 0)        {            start = newNode;            newNode.prev = start;            newNode.next = start;        }        else        {            // Find the last node            temp = (start).prev;                         // Add the last node to make them            // in circular fashion            temp.next = newNode;            newNode.next = start;            newNode.prev = temp;            temp = start;            temp.prev = newNode;        }    }    return start;} // Driver Codepublic static void Main(String []args){    // Array to be converted    int []arr = {1,2,3,4,5};    int n = arr.Length;         // Start Pointer    node start = null;         // Create the List    start = createList(arr, n, start);         // Display the list    displayList(start);}} // This code contributed by Rajput-Ji
<script> // JavaScript program to convert array to// circular doubly linked list // Doubly linked list nodeclass node{    constructor()    {        this.data=0;        this.next=this.prev=null;    }} // Utility function to create a node in memoryfunction getNode(){    return new node();} // Function to display the listfunction displayList(temp){    let t = temp;    if(temp == null)        return 0;    else    {        document.write("The list is: ");                   while(temp.next != t)        {            document.write(temp.data+" ");            temp = temp.next;        }                   document.write(temp.data);                   return 1;    }} // Function to convert array into listfunction createList(arr,n,start){    // Declare newNode and temporary pointer    let newNode,temp;    let i;           // Iterate the loop until array length    for(i = 0; i < n; i++)    {        // Create new node        newNode = getNode();                   // Assign the array data        newNode.data = arr[i];                   // If it is first element        // Put that node prev and next as start        // as it is circular        if(i == 0)        {            start = newNode;            newNode.prev = start;            newNode.next = start;        }        else        {            // Find the last node            temp = (start).prev;                           // Add the last node to make them            // in circular fashion            temp.next = newNode;            newNode.next = start;            newNode.prev = temp;            temp = start;            temp.prev = newNode;        }    }    return start;} // Driver Codelet arr=[1,2,3,4,5];let n = arr.length;// Start Pointerlet start = null; // Create the Liststart = createList(arr, n, start); // Display the listdisplayList(start);  // This code is contributed by avanitrachhadiya2155 </script>
The list is: 1 2 3 4 5
 
andrew1234
Rajput-Ji
avanitrachhadiya2155
Arrays
circular linked list
doubly linked list
Linked Lists
Advanced Data Structure
Arrays
circular linked list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ordered Set and GNU C++ PBDS
2-3 Trees | (Search, Insert and Deletion)
Extendible Hashing (Dynamic approach to DBMS)
Suffix Array | Set 1 (Introduction)
Difference between Backtracking and Branch-N-Bound technique
Quad Tree
Advantages of Trie Data Structure
Implement a Phone Directory
String hashing using Polynomial rolling hash function