Circular Doubly Linked List

The circular doubly linked list containing feature of both double and circular linked list. In this, the last next node of last element contains pointer to the first node and the previous pointer of first element points to the last element. It means we can traverse circular in both forward and backward direction. It has no end. The disadvantage of this linked list is that it takes extra memory space to store previous and next reference of nodes.


Circular Doubly Linked list

The circular linked list basically used in advanced data structures.




Circular Doubly Linked List Program in C

Here is the circular linked list program to create, insert and display list elements.

#include<stdio.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node node; 
node *root=NULL; 
node *create_node(int); 
void insert_elements(int);
void list_elements(node *); 
void main()
{
clrscr();
list_elements(root);
insert_elements(16);
insert_elements(32);
insert_elements(10);
insert_elements(30);
insert_elements(29);
list_elements(root);
getch();
}
node *create_node(int x)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void insert_elements(int x)
{
node *start;
start=root;
if(root==NULL)
{ 
root=create_node(x);
root->next=root;
root->prev=root;
}
else
{
while(start->next!=root)
{
start=start->next;
} 
start->next=create_node(x);
start->next->next=root;
start->next->prev=start;
}
}
void list_elements(node *start)
{
printf("\nThe List is given below:-\n");
if(start==NULL)
{ printf("Circular Linked List is Empty!\n"); }
else
{
while(start->next!=root)
{ printf("%d->",start->data);
start=start->next;
}printf("%d->",start->data);
}
}

Output of the above program

The above program returns the following output -

The List is given below:-
Circular Linked List is Empty!

The List is given below:-
16->32->10->30->29->






Read more articles


General Knowledge



Learn Popular Language