Question: Write C++ code with comments an update function that updates the queue whenever the priority changes. I have this priority queue class. #include #include #include
Write C++ code with comments an update function that updates the queue whenever the priority changes. I have this priority queue class.
#include
#include
#include
#include
#include "Library.h"
using namespace std;
// Node declaration and properties
struct node {
int priority;
int data;
struct node *link;
};
// Class implementing the priority queue
class PQ {
private:
node *front;
public:
PQ() {
front = NULL;
}
// Push function for PQ
void push(int data, int priority) {
node *demo, *q;
demo = new node;
demo->data = data;
demo->priority = priority;
if (front == NULL || priority < front->priority) {
demo->link = front;
front = demo;
}
else {
q = front;
while (q->link != NULL && q->link->priority <= priority)
q = q->link;
demo->link = q->link;
q->link = demo;
}
}
// Method to pop the highest priority element in the queue
void pop() {
node *temp;
if (front == NULL)
cout << "There is no item in the queue." << endl;
else {
temp = front;
front = front->link;
free(temp);
}
}
// Display the elements in the queue
void display() {
node *ptr;
ptr = front;
if (front == NULL)
cout << "Priority Queue is empty" << endl;
else {
cout << "Priority Queue is : " << endl;
cout << "Priority Item" << endl;
while (ptr != NULL) {
cout << ptr->priority << " " << ptr->data << endl;
ptr = ptr->link;
}
}
}
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
