Friday, August 31, 2012

C Program for Linear Queue

/*Insertion And Deletion in Linear Queue*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
  int x[10],rear,front;
  public:
  queue()
  {
   front=1;
   rear= 0;
  }
  void insert(int y)
  {
   if(rear>10)cout<<"Overflow";
   x[++rear]=y;
  }
  int del()
  {
   if(front<=1||front>=10){cout<<"underflow";exit(0);}
   return x[front++];
  }
  void process()
  {
    while(1)
    {
//      clrscr();
      cout<<"\nEnter Choice\n 1.Insert  2.Delete  3.Exit";
      char ch;
      int t;
      ch=getch();
      switch(ch)
      {
    case '1':
    cout<<"\nEnter no.:";
    cin>>t;
    insert(t);
    cout<<"Elements in Queue.:";
    for(int i=front;i<=rear;i++)
    cout<<" "<<x[i];
    break;
    case '2':
    t=del();
    cout<<"\nElement POPED.:"<<t;
    cout<<"\nElements in Queue.:";
    for(i=front;i<=rear;i++)
    cout<<" "<<x[i];
    break;
    default:
    exit(0);
     }
     getch();
    }
  }
};
void main()
{
  clrscr();
  queue q;
  q.process();
}

No comments:

Post a Comment