在本练习中,我应该创建一个由向量支持的优先级队列类。除了最后的 Merge 功能外,此代码有效。如何从一和二创建类的新实例,并返回该类的实例?
#include "pqueue-vector.h"
#define PR(x) cout << #x " = " << x << "\n";
using namespace std;
VectorPQueue::VectorPQueue() {
Vector<string> pQueue;
}
VectorPQueue::~VectorPQueue() {
}
string VectorPQueue::peek() const {
if(isEmpty()) {
error("peek; Attempting to peek at an empty queue");
}
string result = pQueue.get(0);
return result;
}
string VectorPQueue::extractMin() {
if (isEmpty()) {
error("extractmin; Queue is empty");
}
int min = 0;
string minimum = pQueue.get(0);
for (int i= 1; i < logSize; i++) {
string check = pQueue.get(i);
if (check < minimum) {
min = i;
minimum = check;
}
}
pQueue.remove(min);
logSize--;
return minimum;
}
void VectorPQueue::enqueue(const string& elem) {
pQueue.add(elem);
logSize++;
}
VectorPQueue *VectorPQueue::merge(VectorPQueue *one, VectorPQueue *two) {
VectorPQueue result;
for (int i = 0; i < one->size(); i++) {
string test1 = one->extractMin();
string test2 = two->extractMin();
result.enqueue(test1);
result.enqueue(test2);
}
// placeholder so method compiles..
// replace with your own implementation
return new VectorPQueue result;
}