Site icon DataFlair

Circular Queue in Data Structure using C++

Program 1

#include<iostream>
#define MAXSIZE 10
#define clrscr() system("cls")
using namespace std;
class MyCqueue
{
    private:
    int rear,front;
    int cq[MAXSIZE];
    public:
    MyCqueue()
    {
        rear=-1;
        front=-1;
    }
   void insert();
   void delete1();
   void display();
};
void MyCqueue::insert()
{
       int n;
    if ((rear+1)%MAXSIZE==front)
         cout<<"\n Queue is overflow";
    else
    {
       cout<<"\nEnter element: ";
       cin>>n;
       if(front==-1 && rear==-1)
           front=rear=0;
       else
       rear=(rear+1)%MAXSIZE;

       cq[rear]=n;    
    }     
}
void MyCqueue::delete1()
{
       int n;
         if(rear==-1 && front==-1)
            cout<<"\n Queue is empty";
         else   
         {
             n=cq[front];
             if(front==rear)             
             {
                front=-1;
                rear=-1;
             }
             else
             front=(front+1)%MAXSIZE;
             cout<<"\n Deleted element: "<<n;
         }
}

void MyCqueue::display()
{
       int i;
     if(rear==-1 && front==-1)
        cout<<"\n Queue is empty";
         else   
         {
              for(i=front;i!=rear;i=(i+1)%MAXSIZE)
              {
                cout<<"  "<<cq[i];
              }
               cout<<"  "<<cq[i];
         }
}
int main()
{
   clrscr();
   int choice;
 MyCqueue M;

 do
 {
   cout<<"\n--------------Circular Queue Menu---------------------";
   cout<<"\n1. INSERT";
   cout<<"\n2. DELETE";
   cout<<"\n3. DISPLAY";
   cout<<"\n4.EXIT";
   cout<<"\n------------------------------------------------"; 
   cout<<"\nEnter your choice";
   cin>>choice;
   switch(choice)
   {
      case 1:M.insert();break;
      case 2:M.delete1();break;
      case 3:M.display();break;
      case 4:break;
      default:cout<<"\nInvalid choice";  
   }
 }while(choice!=4);   
return 0;
}

 

Exit mobile version