Priority Queue Implementation
A priority queue is an abstract data type. It is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first.
However, if elements with the same priority occur, they are served according to their order in the queue.
The priority queue supports only comparable elements, which means that the elements are either arranged in an ascending or descending order.
For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering imposed on the values is from least to the greatest. Therefore, the 1 number would be having the highest priority while 22 will be having the lowest priority.
Algorithm
Operations associated with a priority queue in C :
- isEmpty(): To check if the queue is empty
- isFull(): To check whether the queue is full or not
- enqueue(): add an item to the rear of the queue.
- dequeue(): remove an item from the front of the queue.
- Peek: get the element at front of the queue.
Implementation of Priority Queue
Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary search tree.
But here, we will be using the structure to implement the priority queue in this tutorial.