Write a program to create a double-ended queue (Dequeue) with the following operations:
a) insert in the beginning
b) insert in the end
c) delete from beginning
d) delete from end
Code for program to create a doubly link list in C++ Programming
# include <iostream.h>
# include <conio.h>
# include <malloc.h>
# include <process.h>
int display_menu();
struct link_list
{
int no;
struct link_list *next;
struct link_list *prev;
};
class dqueue
{
link_list *list,*head;
public:
dqueue()
{
head=NULL;
}
void insert_first();
void insert_last();
void delete_first();
void delete_last();
void display();
};
void dqueue :: insert_first()
{
link_list *newnode;
newnode = new link_list;
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
newnode->next=list;
newnode->prev=NULL;
list->prev=newnode;
head=newnode;
}
void dqueue :: insert_last()
{
link_list *newnode;
newnode= new link_list;
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
while(list->next!=NULL)
{
list=list->next;
}
list->next=newnode;
newnode->prev=list;
newnode->next=NULL;
}
void dqueue :: display()
{
cout<<endl;
link_list *tail;
list=head;
if(head==NULL)
{
cout<<"Empty Queue !!!";
return;
}
cout<<endl;
cout<<"Forward Display ..."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
if(list->next==NULL)
{
tail=list;
}
list=list->next;
}
list=tail;
cout<<endl<<"Backward Display ..."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
list=list->prev;
}
}
void dqueue :: delete_first()
{
list=head;
list->next->prev=NULL;
head=list->next;
}
void dqueue :: delete_last()
{
list=head;
while(list->next->next!=NULL)
{
list=list->next;
}
list->next=NULL;
}
void main()
{
dqueue dq1;
clrscr();
while(1)
{
switch(display_menu())
{
case 1 : dq1.insert_first();
dq1.display();
getch();
break;
case 2 : dq1.insert_last();
dq1.display();
getch();
break;
case 3 : dq1.delete_first();
dq1.display();
getch();
break;
case 4 : dq1.delete_last();
dq1.display();
getch();
break;
case 5 : dq1.display();
getch();
break;
case 6 : exit(0);
}
}
}
int display_menu()
{
int ch;
clrscr();
cout<<"[ 1 ] : Insert at First"<<endl;
cout<<"[ 2 ] : Insert at Last"<<endl;
cout<<"[ 3 ] : Delete From First"<<endl;
cout<<"[ 4 ] : Delete From Last"<<endl;
cout<<"[ 5 ] : Display"<<endl;
cout<<"[ 6 ] : Exit"<<endl;
cout<<"Enter your choice :";
cin>>ch;
return(ch);
}
# include <conio.h>
# include <malloc.h>
# include <process.h>
int display_menu();
struct link_list
{
int no;
struct link_list *next;
struct link_list *prev;
};
class dqueue
{
link_list *list,*head;
public:
dqueue()
{
head=NULL;
}
void insert_first();
void insert_last();
void delete_first();
void delete_last();
void display();
};
void dqueue :: insert_first()
{
link_list *newnode;
newnode = new link_list;
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
newnode->next=list;
newnode->prev=NULL;
list->prev=newnode;
head=newnode;
}
void dqueue :: insert_last()
{
link_list *newnode;
newnode= new link_list;
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
while(list->next!=NULL)
{
list=list->next;
}
list->next=newnode;
newnode->prev=list;
newnode->next=NULL;
}
void dqueue :: display()
{
cout<<endl;
link_list *tail;
list=head;
if(head==NULL)
{
cout<<"Empty Queue !!!";
return;
}
cout<<endl;
cout<<"Forward Display ..."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
if(list->next==NULL)
{
tail=list;
}
list=list->next;
}
list=tail;
cout<<endl<<"Backward Display ..."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
list=list->prev;
}
}
void dqueue :: delete_first()
{
list=head;
list->next->prev=NULL;
head=list->next;
}
void dqueue :: delete_last()
{
list=head;
while(list->next->next!=NULL)
{
list=list->next;
}
list->next=NULL;
}
void main()
{
dqueue dq1;
clrscr();
while(1)
{
switch(display_menu())
{
case 1 : dq1.insert_first();
dq1.display();
getch();
break;
case 2 : dq1.insert_last();
dq1.display();
getch();
break;
case 3 : dq1.delete_first();
dq1.display();
getch();
break;
case 4 : dq1.delete_last();
dq1.display();
getch();
break;
case 5 : dq1.display();
getch();
break;
case 6 : exit(0);
}
}
}
int display_menu()
{
int ch;
clrscr();
cout<<"[ 1 ] : Insert at First"<<endl;
cout<<"[ 2 ] : Insert at Last"<<endl;
cout<<"[ 3 ] : Delete From First"<<endl;
cout<<"[ 4 ] : Delete From Last"<<endl;
cout<<"[ 5 ] : Display"<<endl;
cout<<"[ 6 ] : Exit"<<endl;
cout<<"Enter your choice :";
cin>>ch;
return(ch);
}
0 Comments