Home Uncategorized Write a C++ Program to implement Deque and Deque Operations

Write a C++ Program to implement Deque and Deque Operations

SHARE

AIM: Write a program to implement deque and deque operations.

THEORY:

Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.

The functions for deque are same as vector, with an addition of push and pop operations for both front and back.

SOURCE CODE:

#include<iostream>
#include<deque>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
    deque<int>dq;
    deque<int>::iterator it;
    int choice,item;
    while(1)
    {
        cout<<“1. Insert element in the end”<<endl;
        cout<<“2. Insert element at 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. Least element at deque”<<endl;
        cout<<“7. Size of 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_front();
            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 the Deque\n”;
            for(it=dq.begin();it!=dq.end();it++)
            {
                cout<<*it<<” “;
            }
            cout<<endl;
            break;
            case 9:exit(1);
            break;
            default:cout<<“wrong choice\n”;
        }
    }
}

OUTPUT:

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. size of deque
  8. Display deque
  9. Exit

Enter your choice: 2
Enter value to be inserted at the front: 456

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. size of deque
  8. Display deque
  9. Exit

Enter your choice: 1
Enter value to be inserted at the end: 742

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. size of deque
  8. Display deque
  9. Exit

Enter your choice: 2
Enter value to be inserted at the front: 961

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. Size of deque
  8. Display deque
  9. Exit

Enter your choice: 7
Size of queue is 3

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. size of deque
  8. Display deque
  9. Exit

Enter your choice: 8
Elements of the Deque
961 456 742

  1. Insert element in the end
  2. Insert element at front
  3. Delete element at the end
  4. Delete element at the front
  5. Front element at deque
  6. Least element at deque
  7. size of deque
  8. Display deque
  9. Exit

Enter your choice: 9

Back to Programs.