Circular Doubly Linked List in DSA using C++ Part – 2
by TechVidvan Team
Program 1
// Circular double linked list code
#include<iostream>
#include<stdio.h>
#define clrscr() system("cls")
using namespace std;
class node
{
public:
node *ladd;
int data;
node *radd;
};
int count=0;
node *start=NULL,*new1,*temp,*prv,*nxt;
void create();
void display();
void insertfirst();
void insertmid();
void insertlast();
void delfirst();
void delmid();
void dellast();
int main()
{
int choice;
clrscr();
do
{
cout<<"\n--------------------Circular Double Linked List------------------";
cout<<"\n1.Create";
cout<<"\n2.Display";
cout<<"\n3.Insert First";
cout<<"\n4.Insert Middle";
cout<<"\n5.Insert Last";
cout<<"\n6.Delete First";
cout<<"\n7.Delete Middle";
cout<<"\n8.Delete Last";
cout<<"\n9.Exit";
cout<<"\n-------------------------------------------------------------------";
cout<<"\n Enter your choice";
cin>>choice;
switch(choice)
{
case 1:create();break;
case 2:display();break;
case 3:insertfirst();break;
case 4:insertmid();break;
case 5:insertlast();break;
case 6:delfirst();break;
case 7:delmid();break;
case 8:dellast();break;
case 9:break;
default:cout<<"Invalid choice";
}
}while(choice!=9);
}
void create()
{
int n;
char ch;
cout<<"Enter an element";
cin>>n;
start=new node;
temp=start;
start->data=n;
start->ladd=temp;
start->radd=temp;
count++;
cout<<"want to continue";
cin>>ch;
while(ch=='y'|| ch=='Y')
{
cout<<"Enter an element";
cin>>n;
new1=new node;
new1->data=n;
new1->ladd=temp;
temp->radd=new1;
start->ladd=new1;
new1->radd=start;
temp=temp->radd;
count++;
cout<<"want to continue";
cin>>ch;
}
}
void display()
{
// temp=start;
// cout<<"\nFirst Node: "<<temp->data;
// temp=temp->ladd;
// cout<<"\nLast Node: "<<temp->data;
// temp=temp->radd;
// cout<<"\nFirst Node: "<<temp->data;
if(start==NULL)
cout<<"\nList not found";
else
{
temp=start;
do
{
cout<<temp->data<<" ";
temp=temp->radd;
} while (temp!=start);
cout<<"\n Total Node is List: "<<count;
}
}
void insertfirst()
{
int n;
if(start==NULL)
cout<<"\n List not found....";
else
{
cout<<"\n Enter an element for insert";
cin>>n;
temp=start->ladd;
new1=new node;
new1->data=n;
new1->radd=start;
start->ladd=new1;
start=new1;
start->ladd=temp;
temp->radd=start;
count++;
}
}
void insertmid()
{
int n,pos,i=1;
if(start==NULL)
cout<<"\n List not found";
else
{
cout<<"\nEnter position for insert";
cin>>pos;
if(pos>count)
cout<<"\n Invalid position";
else
{
cout<<"\nEnter an element for insert";
cin>>n;
new1=new node;
new1->data=n;
nxt=start;
while(i<pos)
{
prv=nxt;
nxt=nxt->radd;
i++;
}
prv->radd=new1;
new1->ladd=prv;
new1->radd=nxt;
nxt->ladd=prv;
count++;
}
}
}
void insertlast()
{
int n;
if(start==NULL)
cout<<"\n List not found....";
else
{
cout<<"\n Enter an element for insert";
cin>>n;
new1=new node;
new1->data=n;
temp=start->ladd;
temp->radd=new1;
new1->ladd=temp;
start->ladd=new1;
new1->radd=start;
count++;
}
}
void delfirst()
{
if(start==NULL)
cout<<"\n List not found....";
else
{
nxt=start->ladd;
temp=start;
start=start->radd;
nxt->radd=start;
start->ladd=nxt;
cout<<"Delete node: "<<temp->data;
delete temp;
count--;
}
}
void delmid()
{
int pos,i;
if(start==NULL)
cout<<"\n List not found.....";
else
{
cout<<"\nEnter position for delete";
cin>>pos;
if(pos>count)
cout<<"\n Invalid position";
else
{
i=1;
temp=start;
while(i<pos)
{
temp=temp->radd;
i++;
}
prv=temp->ladd;
nxt=temp->radd;
prv->radd=nxt;
nxt->ladd=prv;
cout<<"\n Deleted node: "<<temp->data;
delete temp;
count--;
}
}
}
void dellast()
{
if(start==NULL)
cout<<"\n List not found...";
else
{
temp=start->ladd;
prv=temp->ladd;
start->ladd=prv;
prv->radd=start;
cout<<"\n Deleted node : "<<temp->data;
delete temp;
count--;
}
}
Tags: circular doubly linked listcircular doubly linked list in dsa using c++Data Structuresdata structures using c++dsa using c++dsa using c++ practicaldsa using c++ programdsa with c++
TechVidvan Team
TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.