Abstract Data Types C++ The following questions all involve PriQueueInterface, w
ID: 3908096 • Letter: A
Question
Abstract Data Types C++
The following questions all involve PriQueueInterface, which contains all of the functions required to implement a priority queue, based entirely on the int type.
i. Write the definition for PriQueueInterface. You may assume that your queue insertion function takes an additional int parameter priority.
ii. Define a class called PriVectorQueue which inherits from PriQueueInterface. PriVectorQueue uses a vector for its queue. Insertion into the priority queue is based on the ’highest priority served first’ approach.
iii. Your manager asks you to add a bypassQueue function as a public function that allows users to select and remove an entry from anywhere in the queue. Is this a good or a bad idea? Discuss your answer in detail.
Explanation / Answer
struct item{
int data;
int priority;
};
class PriQueueInterface {
private:
item *data;
int max;
int size;
public:
PriQueueInterface(){
data = new int[100];
max = 100;
size = 0;
}
enqueue(int a, int pri){
if (size < max)(
int found = 0;
for (int i = 0; i<size; i++){
if (data[i].priority < pri){
found = 1;
for(int j = size; j>= i; j--){
data[j+1].data = data[j].data;
data[j+1].priority = data[j].priority;
}
data[i].data = a;
data[i].priority = pri;
size++;
}
}
if (found == 1){
data[size].priority = pri;
data[size].data = a;
}
}
}
int dequeue(int a, int pri){
int a = data[0].data;
for (int i = 1; i<sixe; i++){
data[i-1].data = data[i].data;
data[i-1].priority = data[i].priority;
}
size--;
return a;
}
void print(){
for (int i = 0; i<sixe; i++){
cout << data[i-1].data << " " << data[i].priority << endl;
}
}
}
struct item{
int data;
int priority;
};
class PriVectorQueue: public PriQueueInterface {
private:
vector<item> data1;
int max;
int size;
public:
PriQueueInterface(){
max = 100;
size = 0;
}
enqueue(int a, int pri){
if (size < max)(
int found = 0;
for (int i = 0; i<size; i++){
if (data[i].priority < pri){
found = 1;
for(int j = size; j>= i; j--){
data[j+1].data = data[j].data;
data[j+1].priority = data[j].priority;
}
data[i].data = a;
data[i].priority = pri;
size++;
}
}
if (found == 1){
item b;
b.data = a;
b.priority = b;
data.push_back(b);
}
}
}
void bypassQueue(){
cout << "Enter an index to remive:";
int n;
cin >> n;
if (n >= 0 || n<size){
for (int i = n+1; i<size; i++){
data[i-1].data = data[i].data;
data[i-1].priority = data[i].priority;
}
}
}
}
There is nothing bad about this idea. It just gives freedom to the user to delete
any element form the queue from anywhere.