Wednesday, March 28, 2018

Write a C++ program to implement deque and deque operations.


AIM: Write a C++ 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<<"\n ...."<<endl;
                        cout<<"\n deque implementation is stl"<<endl;
                        cout<<"\n ....."<<endl;
                        cout<<"\n 1.Insert element in the end"<<endl;
                        cout<<"\n 2.Insert element at front"<<endl;
                        cout<<"\n 3,Delete element at the end"<<endl;
                        cout<<"\n 4.Delete element at the front"<<endl;
                        cout<<"\n 5.Front element at deque"<<endl;
                        cout<<"\n 6.Least element at deque"<<endl;
                        cout<<"\n sieof deque"<<endl;
                        cout<<"\n 8.Display deque"<<endl;
                        cout<<"\n 9.Exit"<<endl;
                        cout<<"enter your choice"<<endl;
                        cin>>choice;
                        switch(choice) {
                                    case 1:cout<<"\n Enter value to be inserted at the end";
                                          cin>>item;
                                          dq.push_back(item);
                                          break;
                                    case 2:cout<<"\n Enter value to be inserted at the front" ;
                                           cin>>item;
                                                   dq.push_front(item);
                                                   break;
                                    case 3:item=dq.back();
                                           dq.pop_front();
                                                   cout<<"\n Element"<<item<<"deleted"<<endl;
                                                   break;
                                    case 4:item=dq.front();
                                            dq.pop_front();
                                                    cout<<"\n element"<<item<<"deleted"<<endl;
                                                            break;
                            case 5:cout<<"\n Front elements of the deque";
                                          cout<<dq.front()<<endl;
                                                  break;
                        case 6:cout<<"\n Back elements of the deque";
                                          cout<<dq.back()<<endl;
                                                  break;
                        case 7:cout<<"\n size of the deque"<<dq.size()<<endl;
                                           break;
                        case 8:cout<<"\n Element of the deque";
                                           for(it=dq.begin();it!=dq.end();it++)
                                                   {
                                                            cout<<*it<<" ";
                                                            cout<<endl;
                                                            break;
                                                    }       
                        case 9:exit(1);
                                           break;
                                    default:"wrong choice";                                                                                                                   
                        }
            }
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home