This C++ Program demonstrates implementation of Deque in STL.
Here is source code of the C++ Program to demonstrate Deque in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Implement Deque in Stl
*/
#include <iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
deque<int> dq;
deque<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Deque Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element at the End"<<endl;
cout<<"2.Insert Element at the Front"<<endl;
cout<<"3.Delete Element at the End"<<endl;
cout<<"4.Delete Element at the Front"<<endl;
cout<<"5.Front Element at Deque"<<endl;
cout<<"6.Last Element at Deque"<<endl;
cout<<"7.Size of the Deque"<<endl;
cout<<"8.Display Deque"<<endl;
cout<<"9.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted at the end: ";
cin>>item;
dq.push_back(item);
break;
case 2:
cout<<"Enter value to be inserted at the front: ";
cin>>item;
dq.push_front(item);
break;
case 3:
item = dq.back();
dq.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 4:
item = dq.front();
dq.pop_front();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the Deque: ";
cout<<dq.front()<<endl;
break;
case 6:
cout<<"Back Element of the Deque: ";
cout<<dq.back()<<endl;
break;
case 7:
cout<<"Size of the Deque: "<<dq.size()<<endl;
break;
case 8:
cout<<"Elements of Deque: ";
for (it = dq.begin(); it != dq.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 9:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ g++ deque.cpp $ a.out --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 1 Enter value to be inserted at the end: 9 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 2 Enter value to be inserted at the front: 8 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 1 Enter value to be inserted at the end: 7 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 1 Enter value to be inserted at the end: 6 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 1 Enter value to be inserted at the end: 5 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 2 Enter value to be inserted at the front: 10 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 2 Enter value to be inserted at the front: 7 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 7 Size of the Deque: 7 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 8 Elements of Deque: 7 10 8 9 7 6 5 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 5 Front Element of the Deque: 7 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 6 Back Element of the Deque: 5 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 3 Element 5 deleted --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 8 Elements of Deque: 7 10 8 9 7 6 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 4 Element 7 deleted --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 7 Size of the Deque: 5 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 8 Elements of Deque: 10 8 9 7 6 --------------------- Deque Implementation in Stl --------------------- 1.Insert Element at the End 2.Insert Element at the Front 3.Delete Element at the End 4.Delete Element at the Front 5.Front Element at Deque 6.Last Element at Deque 7.Size of the Deque 8.Display Deque 9.Exit Enter your Choice: 9 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Design & Analysis of Algorithms MCQ
- Check Programming Books
- Practice Programming MCQs
- Check Data Structure Books
- Apply for Computer Science Internship