Convert an Array to a Circular Doubly Linked List
Last Updated :
23 Jun, 2022
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 maintaining 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 the above idea:
C++
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node* getNode()
{
return (( struct node *) malloc ( sizeof ( struct node)));
}
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;
}
}
void createList( int arr[], int n, struct node **start)
{
struct node *newNode,*temp;
int i;
for (i=0;i<n;i++)
{
newNode = getNode();
newNode->data = arr[i];
if (i==0)
{
*start = newNode;
newNode->prev = *start;
newNode->next = *start;
}
else
{
temp = (*start)->prev;
temp->next = newNode;
newNode->next = *start;
newNode->prev = temp;
temp = *start;
temp->prev = newNode;
}
}
}
int main()
{
int arr[] = {1,2,3,4,5};
int n = sizeof (arr) / sizeof (arr[0]);
struct node *start = NULL;
createList(arr, n, &start);
displayList(start);
return 0;
}
|
Java
class GFG
{
static class node
{
int data;
node next;
node prev;
};
static node getNode()
{
return new node();
}
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 ;
}
}
static node createList( int arr[], int n, node start)
{
node newNode,temp;
int i;
for (i = 0 ; i < n; i++)
{
newNode = getNode();
newNode.data = arr[i];
if (i == 0 )
{
start = newNode;
newNode.prev = start;
newNode.next = start;
}
else
{
temp = (start).prev;
temp.next = newNode;
newNode.next = start;
newNode.prev = temp;
temp = start;
temp.prev = newNode;
}
}
return start;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
node start = null ;
start = createList(arr, n, start);
displayList(start);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def getNode():
return (Node( 0 ))
def 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
def createList(arr, n, start):
newNode = None
temp = None
i = 0
while (i < n):
newNode = getNode()
newNode.data = arr[i]
if (i = = 0 ):
start = newNode
newNode.prev = start
newNode. next = start
else :
temp = (start).prev
temp. next = newNode
newNode. next = start
newNode.prev = temp
temp = start
temp.prev = newNode
i = i + 1
return start
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
start = None
start = createList(arr, n, start)
displayList(start)
|
C#
using System;
class GFG
{
public class node
{
public int data;
public node next;
public node prev;
};
static node getNode()
{
return new node();
}
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;
}
}
static node createList( int []arr,
int n, node start)
{
node newNode,temp;
int i;
for (i = 0; i < n; i++)
{
newNode = getNode();
newNode.data = arr[i];
if (i == 0)
{
start = newNode;
newNode.prev = start;
newNode.next = start;
}
else
{
temp = (start).prev;
temp.next = newNode;
newNode.next = start;
newNode.prev = temp;
temp = start;
temp.prev = newNode;
}
}
return start;
}
public static void Main(String []args)
{
int []arr = {1,2,3,4,5};
int n = arr.Length;
node start = null ;
start = createList(arr, n, start);
displayList(start);
}
}
|
Javascript
<script>
class node
{
constructor()
{
this .data=0;
this .next= this .prev= null ;
}
}
function getNode()
{
return new node();
}
function 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 createList(arr,n,start)
{
let newNode,temp;
let i;
for (i = 0; i < n; i++)
{
newNode = getNode();
newNode.data = arr[i];
if (i == 0)
{
start = newNode;
newNode.prev = start;
newNode.next = start;
}
else
{
temp = (start).prev;
temp.next = newNode;
newNode.next = start;
newNode.prev = temp;
temp = start;
temp.prev = newNode;
}
}
return start;
}
let arr=[1,2,3,4,5];
let n = arr.length;
let start = null ;
start = createList(arr, n, start);
displayList(start);
</script>
|
Output:
The list is: 1 2 3 4 5
Time Complexity: O(n), as we are using a loop to traverse n times. Where n is the number of nodes in the linked list.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...