-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityqueue.cpp
84 lines (67 loc) · 2.38 KB
/
priorityqueue.cpp
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
#include <iostream>
#include <list>
using namespace std;
// Class representing an item in the priority queue
template <typename T>
class PriorityQueueItem {
public:
T data;
int priority;
PriorityQueueItem(const T& item, int priority) : data(item), priority(priority) {}
};
// Operator overloading to compare PriorityQueueItems based on priority
template <typename T>
bool operator<=(const PriorityQueueItem<T>& lhs, const PriorityQueueItem<T>& rhs) {
return lhs.priority <= rhs.priority;
}
// Class representing a Priority Queue using an ordered list
template <typename T>
class PriorityQueue {
private:
list<PriorityQueueItem<T>> itemList;
public:
// Function to add an item to the priority queue
void enqueue(const T& item, int priority) {
PriorityQueueItem<T> newItem(item, priority);
// Find the correct position based on priority
auto it = itemList.begin();
while (it != itemList.end() && !(newItem <= *it)) {
++it;
}
// Insert the item at the correct position
itemList.insert(it, newItem);
cout << "Item \"" << item << "\" with priority " << priority << " added to the queue.\n";
}
// Function to remove the highest-priority item from the queue
void dequeue() {
if (itemList.empty()) {
cout << "Queue is empty. No item to dequeue.\n";
} else {
cout << "Dequeued item \"" << itemList.front().data << "\" with priority "
<< itemList.front().priority << ".\n";
itemList.pop_front();
}
}
// Function to display the items in the priority queue
void displayQueue() {
if (itemList.empty()) {
cout << "Queue is empty.\n";
} else {
cout << "Priority Queue Contents:\n";
for (const auto& item : itemList) {
cout << "Item: " << item.data << ", Priority: " << item.priority << endl;
}
}
}
};
int main() {
// Example usage of the PriorityQueue class with integer data type
PriorityQueue<int> intPriorityQueue;
intPriorityQueue.enqueue(10, 2);
intPriorityQueue.enqueue(20, 1);
intPriorityQueue.enqueue(30, 3);
intPriorityQueue.displayQueue();
intPriorityQueue.dequeue();
intPriorityQueue.displayQueue();
return 0;
}