
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert an Array to a Circular Doubly Linked List in C++
In this tutorial, we will be discussing a program to convert an array to a circular doubly linked list.
For this we will be provided with an array. Our task is to take the elements of the array and get it converted into a circular doubly linked list.
Example
#include<iostream> using namespace std; //node structure for doubly linked list struct node{ int data; struct node *next; struct node *prev; }; //node creation struct node* getNode(){ return ((struct node *)malloc(sizeof(struct node))); } //printing the list int print_list(struct node *temp){ struct node *t = temp; if(temp == NULL) return 0; else { cout<<"List: "; while(temp->next != t) { cout<<temp->data<<" "; temp = temp->next; } cout<<temp->data; return 1; } } //converting array to linked list void convert_array(int arr[], int n, struct node **start) { //declaring new pointer struct node *newNode,*temp; int i; //moving through all the elements for(i=0;i<n;i++){ newNode = getNode(); newNode->data = arr[i]; if(i==0) { *start = newNode; newNode->prev = *start; newNode->next = *start; } else { //calculating the last node 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; convert_array(arr, n, &start); print_list(start); return 0; }
output
List: 1 2 3 4 5
- Related Articles
- Searching an Element in Doubly Circular Linked List using C++
- C++ Program to Implement Circular Doubly Linked List
- Doubly Linked List as Circular in Javascript
- Convert singly linked list into circular linked list in C++
- Doubly Linked Circular Lists in C++
- Convert a Binary Tree to a Circular Doubly Link List in C++
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Reverse a Doubly Linked List using C++
- C++ Program to Implement Doubly Linked List
- Python program to convert a given binary tree to doubly linked list
- Python program to search an element in a doubly linked list
- Delete a node in a Doubly Linked List in C++
- Golang Program to Convert Linked list to an Array
- C++ Program to Implement Sorted Doubly Linked List

Advertisements