C++ Program to Implement Deque in STL

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.

  1. /*
  2.  * C++ Program to Implement Deque in Stl
  3.  */
  4. #include <iostream>
  5. #include <deque>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     deque<int> dq;
  12.     deque<int>::iterator it;
  13.     int choice, item;
  14.     while (1)
  15.     {
  16.         cout<<"\n---------------------"<<endl;
  17.         cout<<"Deque Implementation in Stl"<<endl;
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"1.Insert Element at the End"<<endl;
  20.         cout<<"2.Insert Element at the Front"<<endl;
  21. 	cout<<"3.Delete Element at the End"<<endl;
  22.         cout<<"4.Delete Element at the Front"<<endl;
  23.         cout<<"5.Front Element at Deque"<<endl;
  24.         cout<<"6.Last Element at Deque"<<endl;
  25.         cout<<"7.Size of the Deque"<<endl;
  26.         cout<<"8.Display Deque"<<endl;
  27.         cout<<"9.Exit"<<endl;
  28.         cout<<"Enter your Choice: ";
  29.         cin>>choice;
  30.         switch(choice)
  31.         {
  32.         case 1:
  33.             cout<<"Enter value to be inserted at the end: ";
  34.             cin>>item;
  35.             dq.push_back(item);
  36.             break;
  37.         case 2:
  38.             cout<<"Enter value to be inserted at the front: ";
  39.             cin>>item;
  40.             dq.push_front(item);
  41.             break;
  42.         case 3:
  43.             item = dq.back();
  44.             dq.pop_back();
  45. 	    cout<<"Element "<<item<<" deleted"<<endl;
  46.             break;
  47.         case 4:
  48.             item = dq.front();
  49.             dq.pop_front();
  50.             cout<<"Element "<<item<<" deleted"<<endl;
  51.             break;
  52.         case 5:
  53.             cout<<"Front Element of the Deque: ";
  54.             cout<<dq.front()<<endl;
  55.             break;
  56.         case 6:
  57.             cout<<"Back Element of the Deque: ";
  58.             cout<<dq.back()<<endl;
  59.             break;
  60.         case 7:
  61.             cout<<"Size of the Deque: "<<dq.size()<<endl;
  62.             break;
  63.         case 8:
  64.             cout<<"Elements of Deque:  ";
  65.             for (it = dq.begin(); it != dq.end(); it++)
  66.                 cout<<*it<<"  ";
  67.             cout<<endl;
  68.             break;
  69.         case 9:
  70.             exit(1);
  71. 	    break;
  72.         default:
  73.             cout<<"Wrong Choice"<<endl;
  74.         }
  75.     }
  76.     return 0;
  77. }

$ 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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.