Priority Queue in C and C++

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:

  1. 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.
  2. 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

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++

Comment below if you have queries or found any information incorrect in above tutorial for priority queue in C and C++.

1 thought on “Priority Queue in C and C++”

Leave a Comment

Your email address will not be published. Required fields are marked *