Here you will get implementation of priority queue in C and C++ with program example.
Priority Queue is an ordered list of homogeneous elements. In normal queue, service is provided on the basis of First-In-First-Out. In a priority queue service isn’t provided on the basis of First-In-First-Out service, but rather then each element has a priority based on the urgency of the need.
- An element with higher priority is processed before other elements with lower priority.
- Elements with the same priority are processed on First-In-First-Out service basis.
An example of priority queue is a hospital waiting room. A patient having a more fatal problem would be admitted before other patients.
Other applications of priority queues are found in long term scheduling of jobs processed in a computer. In practice, short processes are given a priority over long processes as it improves the average response of the system.
Priority Queue can be implemented using a circular array.
As the service must be provided to an element having highest priority, there could be a choice between:
- List is always maintained sorted on priority of elements with the highest priority element at the front. Here, deletion is trivial but insertion is complicated as the element must be inserted at the correct place depending on its priority.
- List is maintained in the FIFO form but the service is provided by selecting the element with the highest priority. Deletion is difficult as the entire queue must be traversed to locate the element with the highest priority. Here, insertion is trivial (at the rear end).
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; } }
Comment below if you have queries or found any information incorrect in above tutorial for priority queue in C and C++.
This program is not based on priority queue because you not assign any priority to element in program