Custom Search

Saturday 16 June 2012

Priority Queue Using c++


Author: Sanif S S 
Site: theeduzone.blogspot.com 
Email: sanifss@gmail.com


#include<stdio.h>
#include<conio.h>

#define max 3
struct queue
{
int p;
int v;
}a[max];
class pqueue
{
int f,r;
public:
  void insertion();
  void deletion();
  void display();
  pqueue()
  { f=r=-1;
  }
}q;
void pqueue::insertion()
{
int val,pr;
 if(r==max-1)
 {
 cout<<"queue is full";
 getch();
 }
else
{ cout<<"enter the value";
  cin>>val;
  cout<<"enter the priority";
  cin>>pr;

  if(f==-1)
  {
  f++;
  r++;

  a[r].p=pr;
  a[r].v=val;
  }
else
{
if(a[r].p
{r++;
a[r].v=val;
a[r].p=pr;
}
else
{
for(int i=f;i<=r;i++)
{
if(a[i].p>pr)
{
for(int j=r;j>=i;j--)
{a[j+1]=a[j];
}
break;
}
}
a[i].p=pr;
a[i].v=val;
r++;
}
}
}
}
void pqueue::deletion()
{
if(f==-1)
{
cout<<"queue is empty";
getch();
}
else
{
cout<<"deleted elment is"<<" "<

if(r==0)
{
f=r=-1;
}
else
{
for(int i=f;i<=r;i++)
{
a[i]=a[i+1];
}
r--;
}
}
}
void pqueue::display()
{
if(f==-1)
{cout<<"\nqueue is empty";
getch();
}
else
{
cout<<"value  priority";
for(int i=f;i<=r;i++)
{
cout<
}
}
}


void main()
{
clrscr();int choice;
char ch;
do
{cout<<"MENU";
cout<<"\n1.insertion";
cout<<"\n2.deletion";
cout<<"\n3.display";
cout<<"\nenter your choice";
cin>>choice;
switch(choice)
{
case 1: q.insertion();
break;
case 2: q.deletion();
   break;
case 3: q.display();
  break;
default : cout<<"invalid choice";
}
cout<<"\ndo you want to continue.if yes press y";
cin>>ch;
}while(ch=='y'||ch=='Y');
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...