#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
class CDLL
{
private:
node *temp;
node *head;
node *newnode;
char ans;
public:
CDLL(); /*constructor*/
void create();
void display();
void insertAtEnd();
void insertAtHead();
void insertAt();
void remove();
int count();
void search();
}s1;
CDLL::CDLL()
{
head=NULL;
}
void CDLL::create()
{
temp=head;
do
{
newnode=new node;
cout<<"Enter numbers to be stored in linked list.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
temp=head;
}
else
{
temp->next=newnode;
temp=temp->next;
temp->next=head;
}
cout<<"\nDo you want to continue? (Y/y or N/n)";
cin>>ans;
}while(ans=='Y'||ans=='y');
}
void CDLL::display()
{
temp=head;
if(head==NULL)
{
cout<<"No data present.";
}
else
{
do
{
cout<<temp->data<<"\n";
temp=temp->next;
}while(temp!=head);
}
}
void CDLL::insertAtEnd()
{
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be entered.";
cin>>newnode->data;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
head->prev=newnode;
cout<<"Element has been inserted at the end.";
}
}
void CDLL::insertAtHead()
{
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be inserted.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
head=newnode;
cout<<"Element has been inserted at the head.";
}
}
void CDLL::insertAt()
{
int position,i;
i=1;
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be inserted.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
temp=head;
cout<<"Head node created.";
}
else
{
cout<<"At which position do you want to insert the element?";
cin>>position;
temp=head;
if(position==2)
{
newnode->prev=head;
newnode->next=head->next;
head->next->prev=newnode;
head->next=newnode;
}
else
{
do
{
i++;
temp=temp->next;
}while(i<(position-1));
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
}
}
}
void CDLL::remove()
{
node *dnode;
int j,i,num;
cout<<"Enter data of the node which you want to delete";
cin>>num;
temp=head;
dnode=NULL;
if(head->data==num)
{
do
{
temp=temp->next;
}while(temp->next!=head);
dnode=head;
head=(temp->next)->next;
((temp->next)->next)->prev=temp;
temp->next=head;
delete(dnode);
}
else
{
do
{
temp=temp->next;
if(((temp->next)->data)==num)
{
dnode=temp->next;
temp->next=(temp->next)->next;
delete(dnode);
cout<<"Node deleted.";
break;
}
}while(temp!=head);
if(temp==head)
{
cout<<"Data not found.";
}
}
}
int CDLL::count()
{
int counter=0;
temp=head;
do
{
counter=counter+1;
temp=temp->next;
}while(temp!=head);
return counter;
}
void CDLL::search()
{
int num;
int counter=0;
temp=head;
cout<<"Enter data you want to search";
cin>>num;
do
{
counter=counter+1;
if(temp->data==num)
{
cout<<"Data found at "<<counter<<" position.";
break;
}
temp=temp->next;
}while(temp!=head);
if(temp==head)
{
cout<<"Data not found";
}
}
int main()
{
CDLL s1;
int i,c;
char ans;
do
{
cout<<"\n1.Create\n2.Display\n3.Insert an element at the end\n4.Insert an element at the head\n5.Insert an element in middle\n6.Delete a node\n7.Count the number of nodes\n8.Search\nEnter choice number.";
cin>>i;
switch(i)
{
case 1:s1.create();
break;
case 2:s1.display();
break;
case 3:s1.insertAtEnd();
break;
case 4:s1.insertAtHead();
break;
case 5:s1.insertAt();
break;
case 6:s1.remove();
break;
case 7: c=s1.count();
cout<<"No: of nodes = "<<c;
break;
case 8:s1.search();
break;
default:cout<<"Invalid choice.";
}
cout<<"\nDo you want to continue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
class CDLL
{
private:
node *temp;
node *head;
node *newnode;
char ans;
public:
CDLL(); /*constructor*/
void create();
void display();
void insertAtEnd();
void insertAtHead();
void insertAt();
void remove();
int count();
void search();
}s1;
CDLL::CDLL()
{
head=NULL;
}
void CDLL::create()
{
temp=head;
do
{
newnode=new node;
cout<<"Enter numbers to be stored in linked list.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
temp=head;
}
else
{
temp->next=newnode;
temp=temp->next;
temp->next=head;
}
cout<<"\nDo you want to continue? (Y/y or N/n)";
cin>>ans;
}while(ans=='Y'||ans=='y');
}
void CDLL::display()
{
temp=head;
if(head==NULL)
{
cout<<"No data present.";
}
else
{
do
{
cout<<temp->data<<"\n";
temp=temp->next;
}while(temp!=head);
}
}
void CDLL::insertAtEnd()
{
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be entered.";
cin>>newnode->data;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
head->prev=newnode;
cout<<"Element has been inserted at the end.";
}
}
void CDLL::insertAtHead()
{
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be inserted.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
head=newnode;
cout<<"Element has been inserted at the head.";
}
}
void CDLL::insertAt()
{
int position,i;
i=1;
newnode=new node;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter data to be inserted.";
cin>>newnode->data;
if(head==NULL)
{
head=newnode;
temp=head;
cout<<"Head node created.";
}
else
{
cout<<"At which position do you want to insert the element?";
cin>>position;
temp=head;
if(position==2)
{
newnode->prev=head;
newnode->next=head->next;
head->next->prev=newnode;
head->next=newnode;
}
else
{
do
{
i++;
temp=temp->next;
}while(i<(position-1));
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
}
}
}
void CDLL::remove()
{
node *dnode;
int j,i,num;
cout<<"Enter data of the node which you want to delete";
cin>>num;
temp=head;
dnode=NULL;
if(head->data==num)
{
do
{
temp=temp->next;
}while(temp->next!=head);
dnode=head;
head=(temp->next)->next;
((temp->next)->next)->prev=temp;
temp->next=head;
delete(dnode);
}
else
{
do
{
temp=temp->next;
if(((temp->next)->data)==num)
{
dnode=temp->next;
temp->next=(temp->next)->next;
delete(dnode);
cout<<"Node deleted.";
break;
}
}while(temp!=head);
if(temp==head)
{
cout<<"Data not found.";
}
}
}
int CDLL::count()
{
int counter=0;
temp=head;
do
{
counter=counter+1;
temp=temp->next;
}while(temp!=head);
return counter;
}
void CDLL::search()
{
int num;
int counter=0;
temp=head;
cout<<"Enter data you want to search";
cin>>num;
do
{
counter=counter+1;
if(temp->data==num)
{
cout<<"Data found at "<<counter<<" position.";
break;
}
temp=temp->next;
}while(temp!=head);
if(temp==head)
{
cout<<"Data not found";
}
}
int main()
{
CDLL s1;
int i,c;
char ans;
do
{
cout<<"\n1.Create\n2.Display\n3.Insert an element at the end\n4.Insert an element at the head\n5.Insert an element in middle\n6.Delete a node\n7.Count the number of nodes\n8.Search\nEnter choice number.";
cin>>i;
switch(i)
{
case 1:s1.create();
break;
case 2:s1.display();
break;
case 3:s1.insertAtEnd();
break;
case 4:s1.insertAtHead();
break;
case 5:s1.insertAt();
break;
case 6:s1.remove();
break;
case 7: c=s1.count();
cout<<"No: of nodes = "<<c;
break;
case 8:s1.search();
break;
default:cout<<"Invalid choice.";
}
cout<<"\nDo you want to continue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}