Our Feeds

Sunday, 13 July 2014

Ajith KP

Circular Queue Implementation n CPP

Circular Queue implementation in C++....



//Coded By AJITH KP
//http://terminalcoders.blogspot.com/
#include <iostream>
using namespace std;
class circQ
{
 int q[100], n, i;
 int ch, cnt, item, FR, RR;
 public:
 circQ()
 {
  cnt = 0;
  FR = RR = 1;
 }
 void read()
 {
  cout<<"Enter the limit: ";
  cin>>n;
  for(i = 0; i < n; i++)
  {
   q[i] = 0;
  }
  menu();
 }
 int menu()
 {
  cout<<"\t\t\tMENU\n";
  cout<<"\t1. Add Element\n";
  cout<<"\t2. Remove Element\n";
  cout<<"\t3. Display Elements\n";
  cout<<"\t4. Exit\n";
  while(ch!=4)
  {
   cout<<"ENTER CHOICE: ";
   cin>>ch;
   switch(ch)
   {
    case 1:
     addQ();
     break;
    case 2:
     delQ();
     break;
    case 3:
     disQ();
     break;
    case 4:
     break;
    default:
     cout<<"Your choice is wrong\n";
     break;
   }
  }
  cout<<"\nExiting...\n";
  return 0;
 }
 int addQ()
 {
  if((RR+1)%n == FR)
  {
   cout<<"\nQueue is full\n";
   return NULL;
  }
  cout<<"Enter Element: ";
  cin>>item;
  q[RR] = item;
  RR = ++RR%n;
  return 0;
 }
 int delQ()
 {
  if(FR == RR)
  {
   cout<<"\nQueue is empty\n";
   return NULL;
  }
  item = q[FR];
  cout<<"\nItem "<<item<<" deleted"<<endl;
  q[(FR%n)] = 0;
  FR = ++FR % n;
  return 0;
 }
 void disQ()
 {
  cout<<"\nQUEUE:";
  for(i = 0; i < n; i++)
  {
   cout<<" "<<q[i];
  }
  cout<<endl;
 }
};

int main()
{
 circQ q;
 q.read();
 cin.get();
}