Friday, 30 September 2011

implementing the queue with all the operations



implementing the queue with all the operations

#include<iostream.h>
#include<conio.h>
template<class T>
class queue
{
 private:
   T *a;
   int f,r,size;
 public:
   queue(int k=40);
   ~queue();
   int isempty();
   int isfull();
   void insert(T x);
   T del();
   T first();
   T last();
   void display();
};
template<class T>
queue<T>::queue(int k)
{
 size=k;
 f=0;
 r=-1;
 a=new T[size];
}
template<class T>
queue<T>::~queue()
{
 delete []a;
}
template<class T>
int queue<T>::isfull()
{
 return(r==size-1);
}
template<class T>
int queue<T>::isempty()
{
 return(r==-1);
}
template<class T>
void queue<T>::insert(T x)
{
 if(isfull())
  cout<<"Ins.is not possible \n";
 else
 {
  r++;
  a[r]=x;
 }
}
template<class T>
T queue<T>::del()
{
 T x;
 if(isempty())
  return(-1);
 else
 {
  x=a[f];
  f++;
 }
 if(f>r)
 {
  f=0;
  r=-1;
 }
 return(x);
}
template<class T>
void queue<T>::display()
{
 if(isempty())

  cout<<"queue is empty\n";
 else
 {
  cout<<"queue is as follows\n";
  for(int i=f;i<=r;i++)
   cout<<a[i]<<endl;
 }

}

template<class T>
T queue<T>::first()
{
 if(isempty())
  return(-1);
 else
  return(a[f]);
}
template<class T>
T queue<T>::last()
{

if(isempty())
  return(-1);
 else
  return(a[r]);
}
void menu()
{
 cout<<"1. insertion\n";
 cout<<"2. deletion\n";
 cout<<"3. display the queu\n";
 cout<<"4. first inserted value\n";
 cout<<"5. last inserted value\n";
}

void main()
{
 int ch,x,k;

 clrscr();
 cout<<"enter size of the queue\n";
 cin>>k;
 queue<int>q(k);
 menu();
 cout<<"enter ur choice\n";
 cin>>ch;
 while(ch<6)
 {
  switch(ch)
  {
   case 1:{
    cout<<"enter value to be inserted\n";
    cin>>x;
    if(x==-1)
     cout<<"invalid number\n";
    else
     q.insert(x);
    break;
    }
   case 2:{
    x=q.del();
    if(x==-1)
     cout<<"queue is empty\n";
    else
     cout<<"deleted value is="<<x<<endl;
    break;
  case 3: {
     q.display();
     break;
     }
  case 4:{
     x=q.first();
     if(x==-1)
     cout<<"queue is empty\n";
    else
     cout<<"first inserted value is="<<x<<endl;
    break;
       }
  case 5:
    x=q.last();
    if(x==-1)
     cout<<"queue is empty\n";
    else
     cout<<"last inserted value is="<<x<<endl;
    break;
       }
      }//switch
   menu();
   cout<<"enter ur choice\n";
   cin>>ch;
  }//while
 }//end of main()


 Queues

Queues operations (or) implementing the Queues

No comments:

Post a Comment