Priority Queue is a data structure to which the intrinsic ordering of elements does determine the results of basic operations.
There are two types of priority queue.They are
1.Ascending Priority Queue
2.Descending Priority Queue
An ascending priority queue is a collection of data to which item can be inserted arbitrarily but from which only smallest item can be removed.
Where as an descending priority queue is similar but allows deletion of only largest item.
Source Code For ascending priority queue
There are two types of priority queue.They are
1.Ascending Priority Queue
2.Descending Priority Queue
An ascending priority queue is a collection of data to which item can be inserted arbitrarily but from which only smallest item can be removed.
Where as an descending priority queue is similar but allows deletion of only largest item.
Source Code For ascending priority queue
--------------------------------------------------
File Name : ascending_priority_queue.cpp
Id:code block 10.05
------------------------------------------
#include<cstdlib>
#include<iostream>
using namespace std;
const int MAX = 5;
int main()
{
int a,head = 0,tail = -1,i,j;
double queue[MAX],temp,item;
while(1){
cout<<"\n***************** MENU *********************\n1.Enque operation\n2.Deque operation\n3.View\n4.Exit\t";
cin>>a;
switch(a)
{
case 1:{
if(tail>=MAX-1)//enqueue operation
cout<<"Queue is full"<<endl;
else
cout<<"Enter item : ";
cin>>item;
tail++;
queue[tail] = item;
}
break;
case 2:{
cout<<"Dequeue operation ...\n";//Dequeue operation
if(head>tail)
cout<<"Queue is full \n";
else
temp = queue[head];
for(i=head;i<=tail;i++)
if(temp>queue[i])
temp = queue[i];
for(i=tail,j=tail;i>=head;i--)
{
if(temp == queue[i])
continue;
else
{
queue[j]=queue[i];
j--;
}
}
cout<<temp<<endl;
head++;
}
break;
case 3:{
cout<<"\nItem on queue ...\n";//display
for(int i = head; i<=tail;i++)
cout<<endl<<queue[i];
}
cout<<endl;
break;
case 4://end program
exit(0);
break;
}
}
return 0;
}
File Name : ascending_priority_queue.cpp
Id:code block 10.05
------------------------------------------
#include<cstdlib>
#include<iostream>
using namespace std;
const int MAX = 5;
int main()
{
int a,head = 0,tail = -1,i,j;
double queue[MAX],temp,item;
while(1){
cout<<"\n***************** MENU *********************\n1.Enque operation\n2.Deque operation\n3.View\n4.Exit\t";
cin>>a;
switch(a)
{
case 1:{
if(tail>=MAX-1)//enqueue operation
cout<<"Queue is full"<<endl;
else
cout<<"Enter item : ";
cin>>item;
tail++;
queue[tail] = item;
}
break;
case 2:{
cout<<"Dequeue operation ...\n";//Dequeue operation
if(head>tail)
cout<<"Queue is full \n";
else
temp = queue[head];
for(i=head;i<=tail;i++)
if(temp>queue[i])
temp = queue[i];
for(i=tail,j=tail;i>=head;i--)
{
if(temp == queue[i])
continue;
else
{
queue[j]=queue[i];
j--;
}
}
cout<<temp<<endl;
head++;
}
break;
case 3:{
cout<<"\nItem on queue ...\n";//display
for(int i = head; i<=tail;i++)
cout<<endl<<queue[i];
}
cout<<endl;
break;
case 4://end program
exit(0);
break;
}
}
return 0;
}
nice code
ReplyDelete