Thursday 29 November 2012

Circular queue operations


/*Program to impelement Circular queue operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 5

class cqueue
{
 int a[MAX],front,rear;
 public :
 void insert();
 void deletion();
 void display();
 cqueue();
};

void cqueue :: insert()
{
 int val;
 if((front==0 && rear==MAX-1) || (rear+1==front))
 cout<<" Circular Queue is Full";
 else
 {
  if(rear==MAX-1)
  rear=0;
  else
  rear++;
  cout<<"Enter Element to Insert ";
  cin>>val;
  a[rear]=val;
 }
 if(front==-1)
 front=0;
}

void cqueue :: deletion()
{
 int k;
 if(front==-1)
 cout<<"Circular Queue is Empty";
 else
 {
  k=a[front];
  if(front==rear)
  front=rear=-1;
  else
  {
   if(front==MAX-1)
   front=0;
   else
   front++;
  }
  cout<<"Deleted Element :"<<k<<endl;
 }
}

void cqueue :: display()
{
 int i;
 if(front==-1)
 cout<<"Circular Queue is Empty";
 else
 {
  if(rear < front)
  {
   for(i=front;i<=MAX-1;i++)
   cout<<a[i]<<"   ";
   for(i=0;i<=rear;i++)
   cout<<a[i]<<"   ";
  }
  else
  {
   for(i=front;i<=rear;i++)
   cout<<a[i]<<"   ";
   cout<<endl;
  }
 }
}

cqueue::cqueue()
{
 front=rear=-1;
 int choice;
 while(1)
 {
  cout<<"\n\t\t\t    CIRCULAR QUEUE\n";
  cout<<"\t\t1. To Insert an Element in circular Queue\n";
  cout<<"\t\t2. To Delete an Element from circular Queue\n";
  cout<<"\t\t3. To Display the Elements of circular Queue\n";
  cout<<"\t\t4. To Main menu\n";
  cout<<"\t\t5. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1 :
   insert();break;
   case 2 :
   deletion();break;
   case 3 :
   display();break;
   case 4:
 return;
   case 5:
 exit(1);
   default:
  cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }
}
/*=========================7Nov.,2010=============================*/

1 comment:

  1. .thank you for sharing useful post.
    web programming tutorial
    welookups

    ReplyDelete

You are welcome, your comment helps me to make this blog more better.