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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#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++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
#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