Circular Doubly Linked List in DSA C++
by DataFlair Team
Program 1
// Circular Double Linked List
#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
Node *ladd;
int data;
Node *radd;
public:
void create();
void display();
void reverseDisplay();
void insert_first();
void insert_middle();
void insert_last();
void delete_first();
void delete_middle();
void delete_last();
void search_data();
void count_node();
}; Node *start=NULL,*new1,*prev1,*next1,*temp,*last;
void Node:: create()
{
int n;
char choice;
cout<<"\n Enter an element: ";
cin>>n;
start=new Node();
start->ladd=NULL;
start->data=n;
start->radd=NULL;
temp=start;
start->ladd=temp;
temp->radd=start;
cout<<"\n Want to continue(y/Y): ";
cin>>choice;
while(choice=='y' || choice=='Y')
{
cout<<"\n Enter next element: ";
cin>>n;
new1=new Node();
new1->ladd=NULL;
new1->data=n;
new1->radd=NULL;
temp->radd=new1;
new1->ladd=temp;
temp=temp->radd;
start->ladd=new1;
new1->radd=start;
cout<<"\n Want to continue(y/Y): ";
cin>>choice;
}
}
void Node:: display()
{
if(start==NULL)
cout<<"\n List is empty..";
else
{
temp=start;
do
{
cout<<temp->data<<" ";
temp=temp->radd;
} while (temp!=start);
}
}
void Node:: insert_first()
{
int n;
if(start==NULL)
cout<<"\n List is empty..";
else
{
cout<<"Enter an element for insert: ";
cin>>n;
new1=new Node();
new1->ladd=NULL;
new1->data=n;
new1->radd=NULL;
last=start->ladd;
new1->radd=start;
start->ladd=new1;
start=new1;
start->ladd=last;
last->radd=start;
}
}
void Node:: insert_last()
{
int n;
if(start==NULL)
cout<<"\n List is empty..";
else
{
cout<<"Enter an element for insert: ";
cin>>n;
new1=new Node();
new1->ladd=NULL;
new1->data=n;
new1->radd=NULL;
last=start->ladd;
last->radd=new1;
new1->ladd=last;
start->ladd=new1;
}
}
void Node::insert_middle()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
int n,pos,i=1;
cout<<"\n Enter an element: ";
cin>>n;
new1=new Node();
new1->data=n;
new1->ladd=NULL;
new1->radd=NULL;
cout<<"\n Enter poistion of Middle: ";
cin>>pos;
next1=start;
while(i<pos)
{
prev1=next1;
next1=next1->radd;
i++;
}
prev1->radd=new1;
new1->ladd=prev1;
new1->radd=next1;
next1->ladd=prev1;
}
}
void Node::delete_first()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
temp=start;
last=start->ladd;
start=start->radd;
start->ladd=last;
last->radd=start;
cout<<"\n Deleted node is : "<<temp->data;
delete temp;
}
}
void Node::delete_last()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
temp=start->ladd;
last=temp->ladd;
start->ladd=last;
last->radd=start;
cout<<"\n Deleted node is : "<<temp->data;
delete temp;
}
}
void Node::delete_middle()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
int pos,i=1;
cout<<"\n Enter the node poistion for delete: ";
cin>>pos;
next1=start;
while(i<pos)
{
prev1=next1;
next1=next1->radd;
i++;
}
temp=next1;
next1=next1->radd;
prev1->radd=next1;
next1->ladd=prev1;
cout<<"\n Deleted node is : "<<temp->data;
delete temp;
}
}
void Node::search_data()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
int s,flag=0;
cout<<"\n Enter an element for search: ";
cin>>s;
temp=start;
do
{
if(s==temp->data)
{
flag=1;
break;
}
temp=temp->radd;
}while(temp!=start);
if(flag==1)
cout<<"\n **** Searching success *****";
else
cout<<"\n **** Searching not success *****";
}
}
void Node::count_node()
{
if(start==NULL)
cout<<"\n List is empty";
else
{
int count=0;
temp=start;
do
{
count++;
temp=temp->radd;
} while (temp!=start);
cout<<"\n Total node is :"<<count;
}
}
int main()
{
system("cls");
int choice;
Node obj;
do
{
cout<<"\n------------Circluar Double Linked List Menu-----------------------";
cout<<"\n 1. Create";
cout<<"\n 2. Display";
cout<<"\n 3. Insert First";
cout<<"\n 4. Insert Middle";
cout<<"\n 5. Insert Last";
cout<<"\n 6. Delete First";
cout<<"\n 7. Delete Middle";
cout<<"\n 8. Delete Last";
cout<<"\n 9. Search";
cout<<"\n 10. Count Node";
cout<<"\n 11. Exit";
cout<<"\n-------------------------------------------------------";
cout<<"\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:obj.create();break;
case 2:obj.display();break;
case 3:obj.insert_first();break;
case 4:obj.insert_middle();break;
case 5:obj.insert_last();break;
case 6:obj.delete_first();break;
case 7:obj.delete_middle();break;
case 8:obj.delete_last();break;
case 9:obj.search_data();break;
case 10:obj.count_node();break;
case 11:break;
default:cout<<"\n Invalid choice";
}
}while(choice!=11);
return 0;
}
Tags: circular doubly linked listcircular doubly linked list in c++circular doubly linked list in dsa c++dsa c++dsa c++ circular doubly linked listdsa c++ practicaldsa c++ programdsa c++ program on circular doubly linked list
DataFlair Team
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.