C++ program – Implement circular queue ADT using an array

by Nideesh C on April 14, 2011 · 0 comments

in C++




#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class cqueue
{
   int q[5],front,rare;
   public:
      cqueue()
      {
	 front=-1;
	 rare=-1;
      }
      void push(int x)
      {
	 if(front ==-1 && rare == -1)
	 {
	   q[++rare]=x;
	   front=rare;
	   return;
	 }
	 else if(front == (rare+1)%5 )
	 {
	    cout <<" Circular Queue over flow";
	    return;
	 }
	 rare= (rare+1)%5;
	 q[rare]=x;
     }
 
     void pop()
     {
	if(front==-1 && rare==	-1)
	{
	  cout <<"under flow";
	  return;
	}
	else if( front== rare  )
	{
	  front=rare=-1;
	  return;
	}
	front= (front+1)%5;
     }
 
 
 
 
 void display()
    {
      int i;
      if( front <= rare)
      {
	for(i=front; i<=rare;i++)
	cout << q[i]<<" ";
      }
      else
      {
	 for(i=front;i<=4;i++)
	 {
	   cout <<q[i] << " ";
	 }
	 for(i=0;i<=rare;i++)
	 {
	    cout << q[i]<< " ";
	 }
      }
    }
};
 
main()
{
 
int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT   2.DELETE   3.DISPLAY    4.EXIT\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
	cin >> ch;
	q1.push(ch); break;
 
case 2: q1.pop(); break;
case 3: q1.display(); break;
case 4: exit(0);
}
}
}

OUTPUT

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice1
 enter element4

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice1
 enter element5

 

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice1
 enter element3

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice3
 4 5 3

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice2

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice3
 5 3

1.INSERT   2.DELETE   3.DISPLAY    4.EXIT
 Enter ur choice4



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ program to implement circular queue using array
  2. C++ program to implement Queue using Linked Representation
  3. C++ program to implement Queue using Formula Based Representation
  4. C++ Program – Array Based Representation of Linear List using templates
  5. C++ program to implement Stack using Formula Based Representation

Leave a Comment

Previous post:

Next post: