Question

In: Computer Science

Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...

Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty.

For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and the remove min() function should be O(n).

Below I will attach the parent class (MPQ.h), child class (unsortedMPQ.h - needs implementation), and unsortedMPQ-main.cpp - for testing and does not need to be implemented.

Please Code in C++.

MPQ.h

#ifndef MPQ_H
#define MPQ_H

//Abstract Minimum Priority Queue Class
template
class MPQ{
public:
virtual T remove_min() = 0;
virtual T min() = 0;
virtual bool is_empty() = 0;
virtual void insert(const T& data) = 0;
};
#endif

unsortedMPQ.h

#ifndef UNSORTEDMPQ_H
#define UNSORTEDMPQ_H

#include
#include
#include "MPQ.h"

/*
* Minimum Priority Queue based on a vector
*/
template
class UnsortedMPQ: MPQ {

};

#endif

unsortedMPQ-main.cpp (for testing)

#include "UnsortedMPQ.h"
#include

using namespace std;

int main() {
{
UnsortedMPQ mpq;
cout << "Inserting 1 - 5" << endl;
mpq.insert(1);
mpq.insert(2);
mpq.insert(3);
mpq.insert(4);
mpq.insert(5);
cout << "Remove min five times" << endl;
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << endl << endl;
}
{
UnsortedMPQ mpq;
cout << "Inserting 5 - 1" << endl;
mpq.insert(5);
mpq.insert(4);
mpq.insert(3);
mpq.insert(2);
mpq.insert(1);
cout << "Remove min five times" << endl;
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << endl << endl;
}
{
UnsortedMPQ mpq;
cout << "Inserting mixed order 1-5" << endl;
mpq.insert(5);
mpq.insert(2);
mpq.insert(4);
mpq.insert(3);
mpq.insert(1);
cout << "Remove min five times" << endl;
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << endl << endl;
}
{
UnsortedMPQ mpq;
cout << "Testing exception" << endl;
try {
mpq.remove_min();
}
catch (exception& e) {
cout << e.what() << endl;
}
cout << endl;
}
{
UnsortedMPQ mpq;
cout << "Inserting mixed order 1-5" << endl;
mpq.insert(5);
mpq.insert(2);
mpq.insert(4);
mpq.insert(3);
mpq.insert(1);
cout << "Remove min five times" << endl;
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << endl;
cout << "Inserting mixed order 11-15" << endl;
mpq.insert(15);
mpq.insert(12);
mpq.insert(14);
mpq.insert(13);
mpq.insert(11);
cout << "Remove min five times" << endl;
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << ", ";
cout << mpq.remove_min() << endl;
cout << "Testing exception" << endl;
try {
mpq.remove_min();
}
catch (exception& e) {
cout << e.what() << endl;
}
}
return 0;
}

Solutions

Expert Solution

Pic 1

Pic 2

Pic 3

Pic 4

Here I had attached 4 pictures of coding in C++ compiler. You can run and execute once as same as i had done. follow all steps one by one.


Related Solutions

Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
What are the differences between a maximum priority queue and a minimum priority queue?
What are the differences between a maximum priority queue and a minimum priority queue?
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
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...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT