Priority Queue
- A priority queue is a special type of queue in which each element is associated with a priority and is served according to its priority.
- If there is a condition when two elements have a same number of a priority then they will be served according to their order.
How priority queue is different from the normal queue?
The difference between Priority Queue and Normal Queue is Normal queue Follows FIFO (first-in-first-out) rule whereas, in a priority queue, the values are removed on the basis of priority of the elements. The element with the highest priority will be removed first.
There are two types of Priority Queues.
1). Ascending Priority Queue
2). Descending priority Queue
Ascending Priority Queue
The element can be inserted arbitrarily but only the smallest element can be removed. For example, suppose there is an array having elements 4, 2, 8 in the same order. So, while inserting the elements, the insertion will be in the same sequence but while deleting, the order will be 2, 4, 8.
Descending priority Queue
The element can be inserted arbitrarily but only the largest element can be removed first from the given Queue. For example, suppose there is an array having elements 4, 2, 8 in the same order. So, while inserting the elements, the insertion will be in the same sequence but while deleting, the order will be 8, 4, 2.
Application of priority queue
- Dijkstra’s algorithm
- Stack implementation
- In CPU scheduling algorithms
- For load balancing and interrupt handling in an operating system
- In Huffman code for data compression
[wpusb]