C++ Menu Driven Program for Queue Operations using Arrays

By | 08.01.2017

C++ Program for Queue Operations using Arrays


Write a C++ Menu Driven Program for Queue Operations using Arrays. Here’s a Simple Program for Queue Operations using Arrays in C++ Programming Language.


What is Queue?


  • A queue is an order collection of items from which items may be deleted at one end (called front or head of the queue) and into which items may be inserted at the other end (called the rear end  or tail of the queue).
  • Queue is a first-in, first-out (FIFO) data structure. i.e. the element added first to the queue will be the one to be removed first.
  • Some of the common terminology associated with queues include ADD/ PUSH and DELETE/ POP of elements to the queue.

ADD/ PUSH refers to adding an element to the queue.

DELETE/ POP refers to removing an element from the queue.


Below is the source code for C++ Menu Driven Program for Queue Operations Using Arrays which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Menu Driven Program for Queue Operations using Arrays  */


#include<iostream>

using namespace std;

class queue
{
  public:
  int q[5],front,rear,x,result;
  void enq();
  void dque();
  void disp();
  queue()
  {
    front=0;
    rear=0;
  }
};
void queue::enq()
{
  if(rear>=5)
  cout<<"\nQueue overflow!!\n";
  else
  {
    cout<<"\nEnter the number to be inserted: ";
    cin>>x;
    rear++;
    q[rear]=x;
    cout<<"\nNumber pushed in the queue:"<<q[rear];
  }
}
void queue::dque()
{
  if(rear==0)
  cout<<"\nQueue underflow!!\n";
  else
  {
    if(front==rear)
    {
      front=0;
      rear=0;
    }
    else
      front++;
  }
  cout<<"\nDeleted element is:";
  result=q[front];
  cout<<result;
}
void queue::disp()
{
  if(rear==0)
    cout<<"\nQueue underflow!!\n";
  else
    cout<<"\nContents of queue is:";
  for(int i=front+1;i<=rear;i++)
    cout<<q[i]<<"\t";
}

int main()
{
  int c;
  queue qu;
 cout<<"\n**********";
 cout<<"QUEUE";
 cout<<"**********\n";
  do
  {
    cout<<"\n1.Insertion\n2.Deletion\n3.Display\n";
    cout<<"\nEnter your choice:";
    cin>>c;
    switch(c)
    {
      case 1:
    qu.enq();
    break;
      case 2:
    qu.dque();
    break;
      case 3:
    qu.disp();
    break;
      default:
    cout<<"\nInvalid choice!!\n";
    }
  }
  while(c<4);
  return 0;
}

OUTPUT : :


// **********QUEUE**********

1.Insertion
2.Deletion
3.Display

Enter your choice:1

Enter the number to be inserted: 1

Number pushed in the queue:1
1.Insertion
2.Deletion
3.Display

Enter your choice:1

Enter the number to be inserted: 2

Number pushed in the queue:2
1.Insertion
2.Deletion
3.Display

Enter your choice:1

Enter the number to be inserted: 3

Number pushed in the queue:3
1.Insertion
2.Deletion
3.Display

Enter your choice:3

Contents of queue is:1  2       3       
1.Insertion
2.Deletion
3.Display

Enter your choice:2

Deleted element is:1
1.Insertion
2.Deletion
3.Display

Enter your choice:3

Contents of queue is:2  3       
1.Insertion
2.Deletion
3.Display

Enter your choice:4

Invalid choice!!
 
Exit code: 0

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….

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments