Wednesday 10 September 2014

C++ program to implement doubly ended queue

Here is a C++ program to implement doubly ended queue using array. In doubly ended queue can insert element by rear as well as by front and delete elements from front as well as rear.


C++ program to implement doubly ended queue

#include<iostream>
using namespace std;
class impdqueue
{
    int a[10],front,rear;
public:
    impdqueue()
    {
        front=-1,rear=-1;
    }
    int irear()
    {
        if(rear<10)
        {
            if(front<0)
                front++;
            rear++;
            cout<<"Enter the element to be inserted ";
            cin>>a[rear];
        }
        else
           cout<<"Overflow";
    }
    void display()
    {
       for(int i=front;i<=rear;i++)
             cout<<a[i]<<" ";
        cout<<endl;
    }
    int ifront()
    {
        if(rear+1>=10)
            cout<<"Overflow";
        else
        {
            front++;
            rear++;
            for(int i=rear;i>front;i--)
                a[i]=a[i-1];
            cout<<"Enter the element to be inserted ";
            cin>>a[front];
        }
    }
    int fdelet()
    {
        if(front>rear)
            cout<<"Empty";
        else
        {
            cout<<"Deleted element is "<<a[front]<<endl;
            front++;
        }
    }
    int rdelet()
    {
        if(front>rear)
            cout<<"Empty";
        else
        {
            cout<<"Deleted element is "<<a[rear]<<endl;
            rear--;
        }
    }
};
int main()
{
    int ch,flag=1;
    impdqueue dqueue;
    while(1)
    {
        cout<<"Enter your choice\n1.)Insert by rear\n2.)Delete by front\n3.)Insert by front\n4.)Delete by rear\n5.)Display\n6.)Exit\nEnter your choice ";
        cin>>ch;
        switch(ch)
        {
        case 1:
             dqueue.irear();
             break;
        case 2:
             dqueue.fdelet();
             break;
        case 3:
             dqueue.ifront();
             break;
        case 4:
             dqueue.rdelet();
             break;
        case 5:
             dqueue.display();
             break;
        case 6:
             flag=0;
        default:
             cout<<"Wrong choice";
        }
    }
}

No comments:

Post a Comment