A priority queue is an abstract data structure where each element has a priority associated with it. Elements are dequeued based on their priority rather than their order of insertion. In some cases, if two elements have the same priority, they follow the First In, First Out (FIFO) order, like a regular queue.
There are two common ways to implement a priority queue: using arrays and linked lists.
In the array-based implementation, elements are stored in an array, and the priority is managed by comparing values or using an auxiliary array for priorities.
#include <iostream>
using namespace std;
class PriorityQueue {
int arr[5]; // Array to store elements
int priority[5]; // Array to store priorities
int size;
public:
PriorityQueue() {
size = 0;
}
// Function to insert an element with a priority
void insert(int value, int pri) {
if (size == 5) {
cout << "Queue is full!\n";
return;
}
arr[size] = value;
priority[size] = pri;
size++;
cout << value << " inserted with priority " << pri << endl;
}
// Function to delete the highest priority element
void deleteHighestPriority() {
if (size == 0) {
cout << "Queue is empty!\n";
return;
}
int maxPriorityIdx = 0;
for (int i = 1; i < size; i++) {
if (priority[i] > priority[maxPriorityIdx]) {
maxPriorityIdx = i;
}
}
cout << "Element with value " << arr[maxPriorityIdx] << " and highest priority " << priority[maxPriorityIdx] << " dequeued.\n";
// Shift elements
for (int i = maxPriorityIdx; i < size - 1; i++) {
arr[i] = arr[i + 1];
priority[i] = priority[i + 1];
}
size--;
}
// Function to peek the highest priority element
int peekHighestPriority() {
if (size == 0) return -1;
int maxPriorityIdx = 0;
for (int i = 1; i < size; i++) {
if (priority[i] > priority[maxPriorityIdx]) {
maxPriorityIdx = i;
}
}
return arr[maxPriorityIdx];
}
};
int main() {
PriorityQueue pq;
pq.insert(10, 2);
pq.insert(20, 5);
pq.insert(30, 1);
cout << "Highest priority element: " << pq.peekHighestPriority() << endl;
pq.deleteHighestPriority();
pq.deleteHighestPriority();
return 0;
}
In the linked list-based implementation, each node contains both the value and its priority. The nodes can either be inserted in a sorted order based on priority, or we can search for the highest priority element during dequeuing.
#include <iostream>
using namespace std;
struct Node {
int data;
int priority;
Node* next;
};
class PriorityQueue {
Node* head;
public:
PriorityQueue() {
head = NULL;
}
// Function to insert an element with priority
void insert(int value, int pri) {
Node* newNode = new Node();
newNode->data = value;
newNode->priority = pri;
newNode->next = NULL;
if (head == NULL || pri > head->priority) {
newNode->next = head;
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL && temp->next->priority >= pri) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
cout << value << " inserted with priority " << pri << endl;
}
// Function to delete the highest priority element
void deleteHighestPriority() {
if (head == NULL) {
cout << "Queue is empty!\n";
return;
}
Node* temp = head;
head = head->next;
cout << "Element with value " << temp->data << " and highest priority " << temp->priority << " dequeued.\n";
delete temp;
}
// Function to peek the highest priority element
int peekHighestPriority() {
if (head == NULL) return -1;
return head->data;
}
};
int main() {
PriorityQueue pq;
pq.insert(10, 2);
pq.insert(20, 5);
pq.insert(30, 1);
cout << "Highest priority element: " << pq.peekHighestPriority() << endl;
pq.deleteHighestPriority();
pq.deleteHighestPriority();
return 0;
}
| Operation | Array-Based Queue | Linked List-Based Queue |
|---|---|---|
| Insert | O(1) | O(n) |
| Delete (Dequeue) | O(n) (search for priority) | O(1) |
| Peek (Highest Priority) | O(n) (search for priority) | O(1) |
| Space Complexity | Fixed size (if using static arrays) | Dynamic (no fixed size) |
Open this section to load past papers