#include <stdio.h>
#include <conio.h>
extern "C" int exit(int st);

// The following defines the queue class.
class  Queue {
 private:
  int size;          //  Max capacity of the queue.
  int rear,front;    //  Indices of the front and rear.
  int *queue;        //  A pointer to an array that holds the contents.
 public:
  Queue(int numb);   // Constructor:initialize variables,allocate space.
 ~Queue();           // Destructor:deallocate space.
  void InQueue(int value);  //  push an integer ,checking for ovarflow.
  int DeQueue();            //  pop an integer.
  int Full();               //  checks if full:non zero if full,zero othewise;
  int Empty();              //  cheks if empty.
  };
// *******************************************************
// The constructure for the queue class.
Queue::Queue(int numb) {
  if (numb<1){
	    fprintf(stderr,"Error:Queue:size%d too small\n",numb);
	    exit(1);
  }
  // Initialze the members of the queue.
  size=numb+1;
  rear=front=0;
  queue=new int[size];    // Allocate an array of integers.
}
// *******************************************************
// The destructor of the queue.
Queue::~Queue(){
 delete [] queue;
}
// *******************************************************
// Put an integer in the queue.
void Queue::InQueue(int value)
{
  if (Full()){
	   fprintf(stderr,"Error:Queue overflow\n");
	   exit(1);
  }
 rear++;
 if (rear==size) rear=0;   // cycle arround.
 queue[rear]=value;
}
// *******************************************************
// Remove an integer from the queue.
int Queue::DeQueue()
{
  if (Empty()) {
       fprintf(stderr,"Error:Queue underflow\n");
       exit(1);
   }
  front++;
  if (front==size) front=0;  // Cycle arround.
  return (queue[front]);
}
// *******************************************************
// Return true if the queue has no more space.
int Queue::Full()

{
  return((rear+1==front) || (rear+1==size && !front));
}
//********************************************************
// Return true if the queue has nothing inside.
int Queue::Empty()
{
return(front==rear);
}
// *******************************************************
// Now this routine asks for the required size of the queue.
// Then it fills the queue with the even numbers starting from zero.
// After that it takes these numbers off and desplays them on the screen.
// The above routine will continue until n or N is pressed.
void main(void)
{
 int size,k,again;
 char q;

 again:
 k=0;clrscr();
 for(k=1;k<4;k++) printf("\n");
 int cout=0;
 printf("             enter size of the queue :  ");
 scanf("%d",&size);
 Queue *queue=new Queue(size);
 while(!queue->Full())
	queue->InQueue((cout++)*2); // puts a number inside.
 while(!queue->Empty())
       printf("  pop %d\n",queue->DeQueue()); // takes it again.
       printf("             You want to continue....If not press n or N : ");
       q=getche(); printf("\n");


   if ((q=='n') || (q=='N')) exit(0); else goto again ;
 exit(0);
}



