IMPLEMENTATION OF CIRCULAR QUEUE USING ARRAYS IN C++


#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 20
class cirq
{
private:int arr[MAX];
  int front,rear,count; 
public:
cirq();
void push(int n);
void pop();
void display();
~cirq();
};
cirq::cirq()
{
front = rear =-1;
count=0;
}
void cirq:: push(int n)
{
if(count<MAX)
{
  if(front ==-1)
  {
   front=rear=0;
  }
else if(rear==MAX-1)  /* if rear is at maximum position then bring to  starting*/
   rear=0;
  else
   rear++;
  arr[rear]=n;
  count++;
}
else
{
  cout<<endl<<"\nqueue is full don't enter now";
}
}
void cirq::pop()
{
if(count>0)
{
  cout<<endl<<"\ndeleted item is"<<arr[front];
  if(front==MAX-1)  /* if front is at maximum position then bring to starting*/
   front=0;
  else
   front++;
  count--;
}
else
  cout<<endl<<"\ncircular queue is empty";
}
void cirq::display()
{
int local = count;
int front1=front;
if(local<0)    /*if there is no element in quque*/
{
  cout<<endl<<"\ncircular queue is empty";
}
else
{
  cout<<endl<<"\ncircular queue elements are: ";
  while(local)
  {
   cout<<endl<<arr[front1];
   if(front1==MAX-1)   /* if front is at maximum position then bring to starting*/
    front1=0;
   else
    front1++;
   local--;
  }
}
}
cirq::~cirq()
{
}
int main()
{
cirq q;
int ch,num;
clrscr();
while(1)
{
cout<<"\n\nMAIN MENU";
cout<<"\n1.INSERTION";
cout<<"\n2.DELETION";
cout<<"\n3.EXIT";
cout<<"\n\nENTER YOUR CHOICE : ";
cin>>ch;
switch(ch)
{
case 1:
 cout<<"\n\nENTER THE ELEMENT : ";
 cin>>num;
 q.push(num);
 q.display();
 break;
case 2:
 q.pop();
 q.display();
 break;
case 3:
  cout<<"\n GOOD BYE!!!!";
  exit(0);
default:
  cout<<"\n\nINVALID CHOICE....";
}
}
getch();
return 0;
}






No comments:

Post a Comment