C++ Menu Driven Program for double ended queue using doubly linked list

By | 09.01.2017

Double ended queue using doubly linked list


Write a C++ Menu Driven Program for double ended queue using doubly linked list. Here’s simple C++ Menu Driven Program for double ended queue using doubly linked list in C++ Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C++ Menu Driven Program for double ended queue using doubly linked list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Menu Driven Program for double ended queue using doubly linked list  */

#include<iostream>
#include<stdlib.h>

using namespace std;
 
class node
{
public:
int data;
class  node *next;
class node *prev;
};
 
class dqueue: public node
{
  node *head,*tail;
  int top1,top2;
  public:
   dqueue()
   {
   top1=0;
   top2=0;
   head=NULL;
   tail=NULL;
   }
  void push(int x){
        node *temp;
        int ch;
        if(top1+top2 >=5)
        {
          cout <<"dqueue overflow";
          return ;
        }
        if( top1+top2 == 0)
          {
            head = new node;
            head->data=x;
            head->next=NULL;
            head->prev=NULL;
            tail=head;
            top1++;
          }
         else
         {
           cout <<" Add element 1.FIRST 2.LAST\n enter ur choice:";
           cin >> ch;
 
 
           if(ch==1)
           {
             top1++;
             temp=new node;
             temp->data=x;
             temp->next=head; 
             temp->prev=NULL;
             head->prev=temp;
             head=temp;
           }
           else
           {
             top2++;
             temp=new node;
             temp->data=x;
             temp->next=NULL;
             temp->prev=tail;
             tail->next=temp;
             tail=temp;
           }
 
         }
  }
  void pop()
  {
   int ch;
   cout <<"Delete 1.First Node 2.Last Node\n Enter ur choice:";
   cin >>ch;
   if(top1 + top2 <=0)
   {
     cout <<"\nDqueue under flow";
     return;
   }
   if(ch==1)
   {
     head=head->next;
     head->prev=NULL;
     top1--;
   }
   else
   {
     top2--;
     tail=tail->prev;
     tail->next=NULL;
   }
  }
 
  void display()
  {
   int ch;
   node *temp;
   cout <<"display from 1.Staring 2.Ending\n Enter ur choice";
   cin >>ch;
   if(top1+top2 <=0)
   {
     cout <<"under flow";
     return ;
   }
   if (ch==1)
   {
    temp=head;
    while(temp!=NULL)
    {
      cout << temp->data <<" ";
      temp=temp->next;
    }
   }
   else
   {
    temp=tail;
    while( temp!=NULL) 
    {
      cout <<temp->data << " ";
      temp=temp->prev;
    }
   }
    }
  };
 
  int main()
  {
    dqueue d1;
    int ch;
    while (1){
        cout <<"1.INSERT  2.DELETE  3.DISPLAU  4.EXIT\n Enter ur choice:";
    cin >>ch; 
    switch(ch)
    {
    case 1:     cout <<"enter element";
                cin >> ch;
                d1.push(ch); break;
    case 2: d1.pop(); break;
    case 3: d1.display(); break;
    case 4: exit(1);
    }
  }
  return 0;
  }

OUTPUT : :


1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:1
enter element3
1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:1
enter element7
 Add element 1.FIRST 2.LAST
 enter ur choice:2
1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:1
enter element9
 Add element 1.FIRST 2.LAST
 enter ur choice:1
1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:1
enter element1
 Add element 1.FIRST 2.LAST
 enter ur choice:1
1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:3
display from 1.Staring 2.Ending
 Enter ur choice1
1 9 3 7 1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:3
display from 1.Staring 2.Ending
 Enter ur choice2
7 3 9 1 1.INSERT  2.DELETE  3.DISPLAU  4.EXIT
 Enter ur choice:4

Above is the source code and output for C++ Menu Driven Program for double ended queue using doubly linked list which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments