Implementation Of Circular Queue in C++

Implementation Of Circular Queue in C++ Using Array 



#include<iostream.h>
#include<conio.h>
#define max 5
class queue{
private:
int front,rear;
int *queue;
public:
queue()
{   cout<<"\nThe size of Queue is Already = 5 or put 5 ";
     cin>>max;

  front=rear=-1;
}
void insert_rear(int n)
{
 if((front==0 && rear==max)||(front==(rear+1)))
 { cout<<"\nQueue is Full\n"; }
 else if(front==-1 && rear==-1)
    {  front=rear=0;
      queue[rear]=n;
    }
    else if ( rear==max && front>0)
    {  rear=0;
       queue[rear]=n;
    }
    else
    {  rear=rear+1;
       queue[rear]=n;
    }

}
void delete_front()
{
 if(front==-1 && rear==-1)
 { cout<<"\n Queue is Empty\n"; }
 else if(front==rear)
 {  front=rear=-1;  }
 else if(front==max)
 {  front=0;    }
 else
 {  front=front+1;
    cout<<"\nItem Deleted  = "<<queue[front-1];
 }
}
void display()
{
 int p;
 if(front==-1 && rear==-1)
 {  cout<<"\nQueue is Empty\n"; }
 else if(front<=max && rear<=front)
   {
 for(p=0;p<=rear;p++)  {
 cout<<queue[p]<<"   ";}
 for(p=front;p<=max;p++){
 cout<<queue[p]<<"   ";        }
 }
 else{
 for(p=front;p<=rear;p++)
 cout<<queue[p]<<"   ";         }
}
};
void main()
{
clrscr();
int a,b;
queue s;
while(getch()!=27)
{
cout<<"\n\t\t\"Select an Option\"\n\n\t||  1  |    Insert  ||\n\t||  2  |    Delete  ||\n\t||  3  |    Display ||";
cin>>a;
clrscr();
switch(a)
{
 case 1:{
cout<<"\nEnter value to be inserted:  ";
cin>>b;
s.insert_rear(b);    break;
}
 case 2:{
       s.delete_front();      break;
}
 case 3:{
       s.display();           break;
}
  }  //switch
   cout<<"\n\n\tAny key to Contenue || Esc to Exit\n";
 }  //while
}  //main



Here its the same code as in above but in this code i put some efforts to show how this circular queue works by shewing boxes and values in them . For batter understanding you must run this code. ☺


#include<iostream.h>
#include<conio.h>
class queue{
private:
int front,rear;
int *queue,max;
public:
queue()
{   cout<<"\nEnter the size of Queue =";
     cin>>max;

  front=rear=-1;
}
void insert_rear(int n)
{
 if((front==0 && rear==max)||(front==(rear+1)))
 { cout<<"\nQueue is Full\n"; }
 else if(front==-1 && rear==-1)
    {  front=rear=0;
      queue[rear]=n;
    }
    else if ( rear==max && front>0)
    {  rear=0;
       queue[rear]=n;
    }
    else
    {  rear=rear+1;
       queue[rear]=n;
    }

}
void delete_front()
{
 if(front==-1 && rear==-1)
 { cout<<"\n Queue is Empty\n"; }
 else if(front==rear)
 {  front=rear=-1;  }
 else if(front==max)
 {  front=0;    }
 else
 {  front=front+1;
    cout<<"\nItem Deleted  = "<<queue[front-1];
 }
}
void display()
{
 int p,z;
 for(int j=1;j<=max+1;j++){
gotoxy(j*5,11);cout<<"|````|";gotoxy(j*5,12);cout<<"|    |";
gotoxy(j*5,13);cout<<"``````";}
 if(front==-1 && rear==-1)
 {  cout<<"\nQueue is Empty\n"; }
 else if(front<=max && rear<=front)
   { z=5;
 for(p=0;p<=rear;p++,z+=5)  {
 gotoxy(z+1,12);cout<<queue[p];}
 z=(front+1)*5;for(p=front;p<=max;p++,z+=5){
 gotoxy(z+1,12);cout<<queue[p];        }
 }
 else{ z=(front+1)*5;
 for(p=front;p<=rear;p++,z+=5){
 gotoxy(z+1,12);cout<<queue[p];        } }
}
};
void main()
{
clrscr();
int a,b;
queue s;
while(getch()!=27)
{
cout<<"\n\t\t\"Select an Option\"\n\n\t||  1  |    Insert  ||\n\t||  2  |    Delete  ||\n\t||  3  |    Display ||";
cin>>a;
clrscr();
switch(a)
{
 case 1:{
cout<<"\nEnter value to be inserted:  ";
cin>>b;
s.insert_rear(b); s.display();   break;
}
 case 2:{
       s.delete_front();  s.display();    break;
}
 case 3:{
       s.display();       s.display();    break;
}
  }  //switch
   cout<<"\n\n\tAny key to Contenue || Esc to Exit\n";
 }  //while
}  //main


0 comments:

Post a Comment

My Instagram