Here you will get usage of the priority queue in C and C++ with program example.
Priority Queue is an arranged rundown of homogeneous components. In a typical queue,
the administration is given based on First-In-First-Out. In a priority, queue administration isn’t
given based on First-In-First-Out help, yet rather then every component has a priority dependent on the desperation of the need.
A component with higher priority is prepared before different components with lower priority.
Components with a similar priority are handled on First-In-First-Out help premise.
An example of a priority queue is an emergency clinic sitting area. A patient having an
increasingly deadly issue would be conceded before different patients.
Different utilizations of priority queues are found in long haul planning of occupations
handled in a PC. By and by, short procedures are given a priority over long procedures as it
improves the normal reaction of the framework.
Priority Queue can be executed utilizing a roundabout cluster.
As the administration must be given to a component having the most noteworthy priority, there could be a decision between:
Rundown is constantly kept up arranged on the priority of components with the most noteworthy priority
component at the front. Here, cancellation is inconsequential yet addition is confused as the component must
be embedded at the right spot contingent upon its priority.
Rundown is kept up in the FIFO structure yet the administration is furnished by
choosing the component with the most elevated priority. Cancellation is troublesome as
the whole queue must be crossed to find the component with the most elevated priority.
Here, addition is insignificant (at the backside).
Program for Priority Queue in C
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct pqueue
{
int data[MAX];
int rear,front;
}pqueue;
void initialize(pqueue *p);
int empty(pqueue *p);
int full(pqueue *p);
void enqueue(pqueue *p, int x);
int dequeue(pqueue *p);
void print(pqueue *p);
void main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
printf("\n1)Create \n2)Insert \n3)Delete \n4)Print \n5)EXIT");
printf("\nEnter Choice: ");
scanf("%d",&op);
switch (op) {
case 1: printf("\nEnter Number of Elements");
scanf("%d",&n );
initialize(&q);
printf("Enter the data");
for(i=0; i<n; i++)
{
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is Full..");
exit(0);
}
enqueue(&q,x);
}
break;
case 2: printf("\nEnter the element to be inserted");
scanf("%d\n",&x);
if(full(&q))
{
printf("\nQueue is Full");
exit(0);
}
enqueue(&q,x);
break;
case 3: if(empty(&q))
{
printf("\nQueue is empty..");
exit(0);
}
x=dequeue(&q);
printf("\nDeleted Element=%d",x);
break;
case 4: print(&q);
break;
default: break;
}
}while (op!=5);
}
void initialize(pqueue *p)
{
p->rear=-1;
p->front=-1;
}
int empty(pqueue *p)
{
if(p->rear==-1)
return(1);
return(0);
}
int full(pqueue *p)
{
if((p->rear+1)%MAX==p->front)
return(1);
return(0);
}
void enqueue(pqueue *p, int x)
{
int i;
if(full(p))
printf("\nOverflow");
else
{
if(empty(p))
{
p->rear=p->front=0;
p->data[0]=x;
}
else
{
i=p->rear;
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX; //anticlockwise movement inside the queue
if((i+1)%MAX==p->front)
break;
}
//insert x
i=(i+1)%MAX;
p->data[i]=x;
//re-adjust rear
p->rear=(p->rear+1)%MAX;
}
}
}
int dequeue(pqueue *p)
{
int x;
if(empty(p))
{
printf("\nUnderflow..");
}
else
{
x=p->data[p->front];
if(p->rear==p->front) //delete the last element
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
void print(pqueue *p)
{
int i,x;
if(empty(p))
{
printf("\nQueue is empty..");
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
printf("\n%d",x);
i=(i+1)%MAX;
}
//prints the last element
x=p->data[i];
printf("\n%d",x);
}
}
Output
1)Create
2)Insert
3)Delete
4)Print
5)EXIT
Enter Choice: 1
Enter Number of Elements4
Enter the data9
12
4
6
1)Create
2)Insert
3)Delete
4)Print
5)EXIT
Enter Choice: 4
12
9
6
4
1)Create
2)Insert
3)Delete
4)Print
5)EXIT
Enter Choice: 3
Deleted Element=12
1)Create
2)Insert
3)Delete
4)Print
5)EXIT
Enter Choice: 5
Program for Priority Queue in C++
#include <iostream>
#include <stdlib.h>
#define MAX 30
using namespace std;
typedef struct pqueue
{
int data[MAX];
int rear,front;
}pqueue;
void initialize(pqueue *p);
int empty(pqueue *p);
int full(pqueue *p);
void enqueue(pqueue *p, int x);
int dequeue(pqueue *p);
void print(pqueue *p);
int main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
cout<<"\n1)Create \n2)Insert \n3)Delete \n4)Print \n5)EXIT";
cout<<"\nEnter Choice: ";
cin>>op;
switch (op) {
case 1: cout<<"\nEnter Number of Elements";
cin>>n;
initialize(&q);
cout<<"Enter the data";
for(i=0; i<n; i++)
{
cin>>x;
if(full(&q))
{
cout<<"\nQueue is Full..";
exit(0);
}
enqueue(&q,x);
}
break;
case 2: cout<<"\nEnter the element to be inserted";
cin>>x;
if(full(&q))
{
cout<<"\nQueue is Full";
exit(0);
}
enqueue(&q,x);
break;
case 3: if(empty(&q))
{
cout<<"\nQueue is empty..";
exit(0);
}
x=dequeue(&q);
cout<<"\nDeleted Element="<<x;
break;
case 4: print(&q);
break;
default: break;
}
}while (op!=5);
return 0;
}
void initialize(pqueue *p)
{
p->rear=-1;
p->front=-1;
}
int empty(pqueue *p)
{
if(p->rear==-1)
return(1);
return(0);
}
int full(pqueue *p)
{
if((p->rear+1)%MAX==p->front)
return(1);
return(0);
}
void enqueue(pqueue *p, int x)
{
int i;
if(full(p))
cout<<"\nOverflow";
else
{
if(empty(p))
{
p->rear=p->front=0;
p->data[0]=x;
}
else
{
i=p->rear;
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX; //anticlockwise movement inside the queue
if((i+1)%MAX==p->front)
break;
}
//insert x
i=(i+1)%MAX;
p->data[i]=x;
//re-adjust rear
p->rear=(p->rear+1)%MAX;
}
}
}
int dequeue(pqueue *p)
{
int x;
if(empty(p))
{
cout<<"\nUnderflow..";
}
else
{
x=p->data[p->front];
if(p->rear==p->front) //delete the last element
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
void print(pqueue *p)
{
int i,x;
if(empty(p))
{
cout<<"\nQueue is empty..";
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
cout<<"\n"<<x;
i=(i+1)%MAX;
}
//prints the last element
x=p->data[i];
cout<<"\n"<<x;
}
}
Remark underneath on the off chance that you have questions or discovered any
data off base in above instructional exercise for priority queue in C and C++.