language is C++ Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an ar
ID: 3804314 • Letter: L
Question
language is C++
Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element,add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in the blank, etc ( Like prior program done with LISTS )
Create a class called Node: Have a Name and Priority.
Data set - 10 is the highest priority, 1 is lowest priority.
Enqueue and dequeue in the following order.
Function Name, Priority
Enqueue Joe, 3
Enqueue Fred,1
Enqueue Tuyet,9
Enqueue Jose, 6
Dequeue
Enqueue Jing, 2
Enqueue Xi, 5
Enqueue Moe, 3
DequeueEnqueue Miko, 7
Enqueue Vlady, 8
Enqueue Frank, 9
Enqueue Anny, 3
DequeueEnqueue Xi, 2
Enqueue Wali, 2
Enqueue xChe, 6
Enqueue xVerra, 8
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Dequeue
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
string name;
int priority;
};
class PriorityQueue {
private:
Node array[10];
int capacity;
int front;
int rear;
public:
PriorityQueue() {
front = 0;
rear = 0;
capacity = 10;
}
void enQueue(string name, int priority) {
if (isFull()) {
cout << "Error : Queue Full" <<endl;
return;
}
Node node;
node.name = name;
node.priority = priority;
array[rear] = node;
rear = (rear+1)%capacity;
displayElements();
}
void deQueue() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
if (rear > front) {
int highPriorityIndex = findMaxPriorityNode(front, rear-1);
shiftElements(highPriorityIndex, rear-1);
}
else {
int indexOne = findMaxPriorityNode(front, capacity-1);
int indexTwo = findMaxPriorityNode(0, rear-1);
if (array[indexOne].priority >= array[indexTwo].priority) {
shiftElements(front, capacity-1);
return;
}
shiftElements(0, rear);
}
rear = (rear-1)%capacity;
displayElements();
}
bool isEmpty() {
if (front == rear)
return true;
return false;
}
bool isFull() {
if ((rear+1)%capacity == front)
return true;
return false;
}
void displayElements() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
cout << "The contents of th Queue are : ";
if (rear > front) {
printElems(front, rear-1);
}
else {
printElems(front, capacity-1);
printElems(0, rear-1);
}
cout << endl;
}
/**
*
* Private helper methods
*/
private:
int findMaxPriorityNode(int start, int end) {
int highPriority = -1;
int highPriorityIndex = -1;
for (int i=start; i<=end; i++) {
Node node = array[i];
if (node.priority > highPriority) {
highPriorityIndex = i;
highPriority = node.priority;
}
}
return highPriorityIndex;
}
void shiftElements(int start, int end) {
for (int i=start; i<end; i++) {
array[i] = array[i+1];
}
}
void printElems(int start, int end) {
for (int i=start; i<=end; i++) {
cout << "["<<array[i].name <<"," << array[i].priority << "] ";
}
}
};
int main()
{
PriorityQueue pq;
pq.enQueue("Joe", 3);
pq.enQueue("Fred", 1);
pq.enQueue("Tuyet", 9);
pq.enQueue("Jose", 6);
pq.deQueue();
pq.enQueue("Jing", 2);
pq.enQueue("Xi", 5);
pq.enQueue("Moe", 3);
pq.deQueue();
pq.enQueue("Miko", 7);
pq.enQueue("Vlady", 8);
pq.enQueue("Frank", 9);
pq.enQueue("Anny", 3);
pq.deQueue();
pq.enQueue("Xi",3);
pq.enQueue("Wali",2);
pq.enQueue("Xche", 6);
pq.enQueue("xVerra", 8);
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
pq.deQueue();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.