Friday 24 November 2017

C++ Program to Implement Doubly Ended Queue


Code:

#include   iostream
#include   cstdlib
using namespace std;
/*
 * Node Declaration
 */
struct node
{
    int info;
    node *next;
    node *prev;

}*head, *tail;

/*
 * Class Declaration
 */
class dqueue
{
    public:
        int top1, top2;
        void insert();
        void del();
        void display();
        dqueue()
        {
            top1 = 0;
            top2 = 0;
            head = NULL;
            tail = NULL;
        }
};

/*
 * Main: Contains Menu
 */
int main()
{
    int choice;
    dqueue dl;
    while (1)
    {
        cout<<"\n-------------"<
        cout<<"Operations on Deque"<
        cout<<"\n-------------"<
        cout<<"1.Insert Element into the Deque"<
        cout<<"2.Delete Element from the Deque"<
        cout<<"3.Traverse the Deque"<
        cout<<"4.Quit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        cout<
        switch(choice)
        {
        case 1:
            dl.insert();
            break;
        case 2:
            dl.del();
            break;
        case 3:
            dl.display();
            break;
        case 4:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;    
}

/*
 * Insert Element in Doubly Ended Queue
 */
void dqueue::insert()
{
    struct node *temp;
    int ch, value;
    if (top1 + top2 >= 50)
    {
        cout<<"Dequeue Overflow"<
        return;
    }
    if (top1 + top2 == 0)
    {
        cout<<"Enter the value to be inserted: ";
        cin>>value;
        head = new (struct node);
        head->info = value;
        head->next = NULL;
        head->prev = NULL;
        tail = head;
        top1++;
        cout<<"Element Inserted into empty deque"<
    }
    else
    {
        while (1)
        {
            cout<
            cout<<"1.Insert Element at first"<
            cout<<"2.Insert Element at last"<
            cout<<"3.Exit"<
            cout<
            cout<<"Enter Your Choice: ";
            cin>>ch;
            cout<
            switch(ch)
            {
            case 1:
                cout<<"Enter the value to be inserted: ";
                cin>>value;  
                temp = new (struct node);
                temp->info = value;
                temp->next = head;
                temp->prev = NULL;
                head->prev = temp;
                head = temp;
                top1++;
                break;
            case 2:
                cout<<"Enter the value to be inserted: ";
                cin>>value; 
                temp = new (struct node);
                temp->info = value;
                temp->next = NULL;
                temp->prev = tail;
                tail->next = temp;
                tail = temp;
                top2++;
                break;
            case 3:
                return;
                break;
            default:
                cout<<"Wrong Choice"<
            }
        }
    }
}

/*
 * Delete Element in Doubly Ended Queue
 */
void dqueue::del()
{
    if (top1 + top2 <= 0)
    {
        cout<<"Deque Underflow"<
        return;
    }
    int ch;
    while (1)
    {
        cout<
        cout<<"1.Delete Element at first"<
        cout<<"2.Delete Element at last"<
        cout<<"3.Exit"<
        cout<
        cout<<"Enter Your Choice: ";
        cin>>ch;
        cout<
        switch(ch)
        {
        case 1:  
            head = head->next;
            head->prev = NULL;
            top1--;
            break;
        case 2:
            tail = tail->prev;
            tail->next = NULL;
            top2--;
            break;
        case 3:
            return;
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
}

/*
 * Display Doubly Ended Queue
 */
void dqueue::display()
{
    struct node *temp;
    int ch;
    if (top1 + top2 <= 0)
    {
        cout<<"Deque Underflow"<
        return;
    }
    while (1)
    {
        cout<
        cout<<"1.Display Deque from Beginning"<
        cout<<"2.Display Deque from End"<
        cout<<"3.Exit"<
        cout<
        cout<<"Enter Your Choice: ";
        cin>>ch;
        cout<
        switch (ch)
        {
        case 1:  
            temp = head;
            cout<<"Deque from Beginning:"<
            while (temp != NULL)
            {
                cout<info<<" ";
                temp = temp->next;                       
            }
            cout<
            break;
        case 2:
            cout<<"Deque from End:"<
            temp = tail;
            while (temp != NULL)
            {
                cout<info<<" ";
                temp = temp->prev;                      
            }
            temp = tail;
            cout<
            break;
        case 3:
            return;
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
}


Output:

-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3

Deque Underflow

-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 2

Deque Underflow

-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 1

Enter the value to be inserted: 100
Element Inserted into empty deque

-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3


1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 1

Deque from Beginning:
100 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 2

Deque from End:
100 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 1


1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 1

Enter the value to be inserted: 200

1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 2       

Enter the value to be inserted: 150

1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3


1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 1

Deque from Beginning:
200 100 150 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 2

Deque from End:
150 100 200 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 2


1.Delete Element at first
2.Delete Element at last
3.Exit

Enter Your Choice: 1


1.Delete Element at first
2.Delete Element at last
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3


1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 1

Deque from Beginning:
100 150 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 2

Deque from End:
150 100 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 1


1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 1

Enter the value to be inserted: 1010

1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 2

Enter the value to be inserted: 1111

1.Insert Element at first
2.Insert Element at last
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3


1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 1

Deque from Beginning:
1010 100 150 1111 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 2

Deque from End:
1111 150 100 1010 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 2


1.Delete Element at first
2.Delete Element at last
3.Exit

Enter Your Choice: 2


1.Delete Element at first
2.Delete Element at last
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 3


1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 1

Deque from Beginning:
1010 100 150 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 2

Deque from End:
150 100 1010 

1.Display Deque from Beginning
2.Display Deque from End
3.Exit

Enter Your Choice: 3


-------------
Operations on Deque

-------------
1.Insert Element into the Deque
2.Delete Element from the Deque
3.Traverse the Deque
4.Quit
Enter your Choice: 4

------------------
(program exited with code: 1)
Press return to continue



More C++ Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...