Thursday, 29 November 2012

Priority Queue operations


/*Program to empelement Priority Queue operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<process.h>

class pqueue
{
 int front,rear,choice;
 public:
 struct data
 {
  int val,p,o;
 }d[MAX];

 void insert();
 void deletion();
 void display();
 pqueue();
};

void pqueue::insert()
{
 data d1;
 if(rear==MAX-1)
 cout<<"Priority Queue is Full";
 else
 {
  cout<<"Enter Value ";
  cin>>d1.val;
  cout<<"Enter Priority ";
  cin>>d1.p;
  cout<<"Enter Order ";
  cin>>d1.o;
  rear++;
  d[rear]=d1;
  if(front==-1)
  front=0;
  data temp;
  for(int i=front;i<=rear;i++)
  for(int j=i+1;j<=rear;j++)
  {
   if(d[i].p > d[j].p)
   {
    temp=d[i];
    d[i]=d[j];
    d[j]=temp;
   }
   else
   {
    if(d[i].p==d[j].p)
     if(d[i].o > d[j].o)
     {
      temp=d[i];
      d[i]=d[j];
      d[j]=temp;
     }
   }
  }
 }
}

void pqueue::deletion()
{
 if(front==-1)
 cout<<"Priority Queue is Empty";
 else
 {
  cout<<"deleted followings...\n";
  cout<<"Value = "<<d[front].val<<endl;
  cout<<"Priority = "<<d[front].p<<endl;
  cout<<"Order = "<<d[front].o<<endl;
  if(front==rear)
  front=rear=-1;
  else
  front++;
 }
}

void pqueue::display()
{
 if(front==-1)
 cout<<"Priority Queue is Empty";
 else
 {
  for(int i=front;i<=rear;i++)
  {
   cout<<"Object  :"<<i+1<<endl;
   cout<<"Value ="<<d[i].val<<endl;
   cout<<"Priority="<<d[i].p<<endl;
   cout<<"Order =  "<<d[i].o<<endl<<endl;
  }
 }
}

pqueue::pqueue()
{
 front=rear=-1;
 while(1)
 {
  int choice;
  cout<<"\n\t\t\t    PRIORITY QUEUE\n";
  cout<<"\t\t1. To Insert an Element in priority Queue\n";
  cout<<"\t\t2. To Delete an Element from priority Queue\n";
  cout<<"\t\t3. To Display the Elements of priority 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";
  }
 }
}
/*=========================8Nov.,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.