Double LinkList in C++
#include<iostream>
#include<conio.h>
using namespace std;
struct node{
int value;
node *previous;
node *next;
};
class doublelist{
node *tail;
node *head;
public:
doublelist()
{
tail=NULL;
head=NULL;
}
void createnode()
{
if(head==NULL)
cout<<endl<<" The List is Empty"<<endl<<endl;
else
cout<<endl<<"List Exist"<<endl<<endl;
node *newnode=new node;
cout<<"Enter the value in the node: "<<endl;
cin>>newnode->value;
newnode->next=NULL;
if(head==NULL)
{
newnode->previous=NULL;
head=newnode;
tail=newnode;
show();
}
else
{
cout<<"Existing List "<<endl<<endl;
show();
tail=head;
while(tail->next!=NULL)
tail=tail->next;
tail->next=newnode; //
newnode->previous=tail; //newnode->pre=
tail=newnode;
show();
}
}
void insert_start()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
cout<<endl;
node *newnode=new node;
cout<<"Enter the value at start: "<<endl;
cin>>newnode->value;
newnode->next=head; //newnode->next=NULL;
newnode->previous=NULL; //newnode->pre=NULL;
head->previous=newnode; //head->pre=newnode;
head=newnode;
cout<<"After Changes : ";
show();
}
}
void insert_position()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
cout<<endl;
int position;
node *pre=new node;
node *cur=new node;
node *newnode=new node;
cout<<"Enter the Position: "<<endl;
cin>>position;
cur=head;
for(int i=1;i<position;i++)
{
pre=cur;
cur=cur->next;
}
cout<<"Enter the value: "<<endl;
cin>>newnode->value;
pre->next=newnode;
newnode->previous=pre;
newnode->next=cur;
cur->previous=newnode;
cout<<"After Changes "<<endl;
show();
}
}
void delete_first()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *newnode=new node;
newnode=head;
head=head->next;
head->previous=NULL;
delete newnode;
cout<<"After Changes : ";
show();
}
}
void delete_last()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *cur=new node;
node *pre=new node;
cur=head;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
tail=pre;
pre->next=NULL;
delete cur;
cout<<"After Changes : ";
show();
}
}
void delete_position()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
int position;
node *temp;
node *cur=new node;
node *pre=new node;
cout<<"Enter the position: "<<endl;
cin>>position;
cur=head;
for(int i=1;i<position;i++)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
temp=cur->next;
temp->previous=cur->previous;
delete cur;
cout<<"After Changes : ";
show();
}
}
void show()
{
node *temp=new node;
temp=head;
cout<<"Values are: ";
while(temp!=NULL)
{
cout<<temp->value<<"\t";
temp=temp->next;
}
}
void update_first()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *temp;
node *newnode=new node;
cout<<"Enter the new value at first: "<<endl;
cin>>newnode->value;
temp=head->next;
head->previous=newnode;
temp->previous=head->previous;
newnode->next=head->next;
head=newnode;
cout<<"After Changes : ";
show();
}
}
void update_last()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *cur=new node;
node *pre=new node;
node *newnode=new node;
cout<<"Enter the new value at Last: "<<endl;
cin>>newnode->value;
cur=head;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
tail=pre;
pre->next=newnode;
newnode->previous=pre;
newnode->next=NULL;
tail=newnode;
cout<<"After Changes : ";
show();
}
}
void update_position()
{
if(head==NULL)
cout<<"List is Empty"<<endl<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
int position;
node *temp;
node *pre=new node;
node *cur=new node;
node *newnode=new node;
cout<<"Enter the position: "<<endl;
cin>>position;
cur=head;
for(int i=1;i<position;i++)
{
pre=cur;
cur=cur->next;
}
cout<<"Enter the value: "<<endl;
cin>>newnode->value;
temp=cur->next;
temp->previous=newnode;
newnode->next=temp;
newnode->previous=pre;
pre->next=newnode;
cout<<"After Changes : ";
show();
}
}
void even_numbers()
{
if(head==NULL)
cout<<"List is Empty"<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *cur=new node;
cur=head;
cout<<"Even Numbers: ";
while(cur!=NULL)
{
if(cur->value%2==0)
cout<<cur->value<<"\t";
cur=cur->next;
}
cout<<"After Changes : ";
show();
}
}
void odd_numbers()
{
if(head==NULL)
cout<<"List is Empty"<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *cur=new node;
cur=head;
cout<<"Odd Numbers: ";
while(cur!=NULL)
{
if(cur->value%2==1)
cout<<cur->value<<"\t";
cur=cur->next;
}
cout<<"After Changes : ";
show();
}
}
void ascending_order()
{
if(head==NULL)
cout<<"List is Empty"<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *i,*j;
int temp;
for(i=head;i->next!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->value>j->value)
{
temp=i->value;
i->value=j->value;
j->value=temp;
}
}
}
i=head;
cout<<"Ascending Oder is : ";
while(i!=NULL)
{
cout<<i->value<<"\t";
i=i->next;
}
cout<<"After Changes : ";
show();
}
}
void decending_order()
{
if(head==NULL)
cout<<"List is Empty"<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
node *i,*j;
int temp;
for(i=head;i->next!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->value<j->value)
{
temp=i->value;
i->value=j->value;
j->value=temp;
}
}
}
i=head;
cout<<"Decending Oder is : ";
while(i!=NULL)
{
cout<<i->value<<"\t";
i=i->next;
}
cout<<"After Changes : ";
show();
}
}
void swap_by_value()
{
if(head==NULL)
cout<<"List is Empty"<<endl;
else
{
cout<<"Existing List "<<endl<<endl;
show();
int m,n,temp;
node *p,*q;
node *cur;
p=head;
q=head;
cout<<"Enter the Position 1st value to Swap: ";
cin>>m;
cout<<"Enter the Position 2nd value to Swap: ";
cin>>n;
for(int i=1;i<m;i++)
{
p=p->next;
}
for(int j=1;j<n;j++)
{
q=q->next;
}
temp=p->value;
p->value=q->value;
q->value=temp;
cur=head;
cout<<"After Swaping Values Are : ";
while(cur!=NULL)
{
cout<<cur->value<<",\t";
cur=cur->next;
}
cout<<"After Changes : ";
show();
}
}
};
int main()
{
int a;
doublelist l1;
while(1)
{
E: system("cls");
cout<<endl<<" *Main Menu* "<<endl<<endl;
cout<<" 1. Create List"<<endl;
cout<<" 2. Insertion Menu"<<endl;
cout<<" 3. Deletion Menu"<<endl;
cout<<" 4. Update Menu"<<endl;
cout<<" 5. Show Menu"<<endl;
cout<<" 6. Swap Menu"<<endl;
cout<<" 7. EXIT"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.createnode();
cout<<endl;
system("pause");
break;
case 2:
while(1)
{
system("cls");
cout<<" *Insertion Menu* "<<endl<<endl;
cout<<" 1. At Start"<<endl;
cout<<" 2. At End"<<endl;
cout<<" 3. At Any Position"<<endl;
cout<<" 4. Back To Main Menu"<<endl;
cout<<" 5. Exit"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.insert_start();
cout<<endl;
system("pause");
break;
case 2:
l1.createnode();
cout<<endl;
system("pause");
break;
case 3:
l1.insert_position();
cout<<endl;
system("pause");
break;
case 4:
goto E;
break;
case 5:
exit(0);
break;
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
case 3:
while(1)
{
system("cls");
cout<<" *Deletion Menu* "<<endl<<endl;
cout<<" 1. At Start"<<endl;
cout<<" 2. At End"<<endl;
cout<<" 3. At Any Position"<<endl;
cout<<" 4. Back To Main Menu"<<endl;
cout<<" 5. Exit"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.delete_first();
cout<<endl;
system("pause");
break;
case 2:
l1.delete_last();
cout<<endl;
system("pause");
break;
case 3:
l1.delete_position();
cout<<endl;
system("pause");
break;
case 4:
goto E;
break;
case 5:
exit(0);
break;
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
case 4:
while(1)
{
system("cls");
cout<<" *Update Menu* "<<endl<<endl;
cout<<" 1. At Start"<<endl;
cout<<" 2. At End"<<endl;
cout<<" 3. At Any Position"<<endl;
cout<<" 4. Back To Main Menu"<<endl;
cout<<" 5. Exit"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.update_first();
cout<<endl;
system("pause");
break;
case 2:
l1.update_last();
cout<<endl;
system("pause");
break;
case 3:
l1.update_position();
cout<<endl;
system("pause");
break;
case 4:
goto E;
break;
case 5:
exit(0);
break;
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
case 5:
while(1)
{
system("cls");
cout<<" *Show Menu* "<<endl<<endl;
cout<<" 1. Ascending Order"<<endl;
cout<<" 2. Decending Order"<<endl;
cout<<" 3. Even Numbers"<<endl;
cout<<" 4. Odd Numbers"<<endl;
cout<<" 5. Back To Main Menu"<<endl;
cout<<" 6. Exit"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.ascending_order();
cout<<endl;
system("pause");
break;
case 2:
l1.decending_order();
cout<<endl;
system("pause");
break;
case 3:
l1.even_numbers();
cout<<endl;
system("pause");
break;
case 4:
l1.odd_numbers();
cout<<endl;
system("pause");
break;
case 5:
goto E;
break;
case 6:
exit(0);
break;
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
case 6:
while(1)
{
system("cls");
cout<<" *Swap Menu* "<<endl<<endl;
cout<<" 1. By Value"<<endl;
cout<<" 2. By Reference"<<endl;
cout<<" 3. Back To Main Menu"<<endl;
cout<<" 4. Exit"<<endl<<endl;
cout<<" Enter your choice : ";
cin>>a;
switch(a)
{
case 1:
l1.swap_by_value();
cout<<endl;
system("pause");
break;
case 2:
l1.swap_by_value();
cout<<endl;
system("pause");
break;
case 3:
goto E;
break;
case 4:
exit(0);
break;
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
case 7:
exit(0);
default:
cout<<"You have entered the wrong option,Enter the right option"<<endl;
}
}
}
Output:
0 Comments