Pages

100 Contemporary Living Room Designs

Wednesday, September 12, 2012

C++ PROGRAM TO IMPLEMENT QUEUE USING ARRAY.


#include<iostream.h>
#include<conio.h>
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";
}
void main()
{
  int c;
  queue qu;
  clrscr();
//  cout<<"\n*****";
//  cout<<"\nQUEUE";
//  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);
  getch();
}


OUTPUT


5 comments:

  1. Its wrong modify the code to dequeue


    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. #include
    #include
    using namespace std;

    class queue
    {
    public:
    int q[5],front,rear,x,result;
    void enq();
    void dque();
    void disp();
    queue()
    {
    front=-1;
    rear=-1;
    }
    };
    void queue::enq()
    {
    if(rear == (5-1))
    cout<<"\nQueue overflow!!\n";
    else
    {
    cout<<"\nEnter the number to be inserted: ";
    cin>>x;
    rear++;
    q[rear]=x;
    cout<<"\nNumber pushed in the queue:"<>c;
    switch(c)
    {
    case 1:
    qu.enq();
    break;
    case 2:
    qu.dque();
    break;
    case 3:
    qu.disp();
    break;
    case 4:
    exit(0);
    default:
    cout<<"\nInvalid choice!!\n";
    }
    }
    while(c!=4);
    return(0);
    }

    ReplyDelete