Sunday, 2 July 2017

C++ program of circular queue

In this program we will see how to insert,delete and show data in Circular Queue.

//Program starts

#include<conio.h>
#include<iostream>
using namespace std;

class cirq{
          private:
                    int d,size,cq[5],counter,front,rear;
          public:
                    cirq();
                    void create();
                    void incq(int d);
                    void delcq();
                    void show();
};

cirq::cirq()
{
          size=5;
          counter=0;
          front=-1;
          rear=-1;
}

void cirq::create()
{
          for(int i=0;i<5;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>cq[i];
                   
          }
          front=0;
          rear=4;
          counter=5;
         
}

void cirq::incq(int d)
{
          if(counter==5)
          {
                    cout<<"Queue is full\n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    rear=(rear+1)%size;
                    cq[rear]=d;
                    counter++;
          }
}

void cirq::delcq()
{
          if(counter==-1)
          {
                    cout<<"Queue is empty\n";
          }
          else
          {
                    front=(front+1)%size;
                    counter--;
          }
}

void cirq::show()
{
          for(int i=0;i<counter;i++)
          {
                    cout<<"Data at ["<<(front+i)%5<<"] = "<<cq[(front+i)%5]<<endl;
          }
}


int main()
{
          cirq c; //Creating Object
          int b,d;
          c.create();    
do{
          cout<<"\n Press 1 to show data\n Press 2 to Delete data\n Press 3 to Insert data \n Press 4 to EXIT the program\nValue= ";
          cin>>b;
          switch(b)
          {
                    case 1:
                              c.show();
                              break;
                    case 2:
                              c.delcq();
                              break;
                    case 3:
                              c.incq(d);
                              break;
                    case 4:
                              cout<<"Bye! Bye!";
return 1;
          }
}
while(1);
         
         
}


OUTPUT:
Share:

0 comments:

Post a Comment