Convert an Array to a Circular Doubly Linked List
Prerequisite: Doubly Linked list, Circular Linked List, Circular Doubly Linked List
Given 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++
// CPP program to convert array to // circular doubly linked list #include<iostream> using namespace std; // Doubly linked list node struct node { int data; struct node *next; struct node *prev; }; // Utility function to create a node in memory struct node* getNode() { return ((struct node *)malloc(sizeof(struct node))); } // Function to display the list int 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 list void 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 Code int 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
// Java program to convert array to // circular doubly linked list class GFG { // Doubly linked list node static class node { int data; node next; node prev; }; // Utility function to create a node in memory static node getNode() { return new node(); } // Function to display the list static 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 list static 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 Code public 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 |
C#
// C# program to convert array to // circular doubly linked list using System; class GFG { // Doubly linked list node public class node { public int data; public node next; public node prev; }; // Utility function to create a node in memory static node getNode() { return new node(); } // Function to display the list static 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 list static 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 Code public 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 |
The list is: 1 2 3 4 5
Recommended Posts:
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Convert a Binary Tree to a Circular Doubly Link List
- Reverse a doubly circular linked list
- Doubly Circular Linked List | Set 2 (Deletion)
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Search an Element in Doubly Circular Linked List
- Insertion at Specific Position in a Circular Doubly Linked List
- Convert singly linked list into circular linked list
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Replace even nodes of a doubly linked list with the elements of array
- Circular Queue | Set 2 (Circular Linked List Implementation)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



