Priority Queue
Sep 16, 2014 at 11:10pm UTC
I'm having trouble wrapping my head around this abstract data type business. I understand how to make a queue and a stack, but this priority queue is throwing me off. I'm supposed to take this header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "queue-provided.h"
const int PQSIZE = 11;
template <class T>
struct PriorityQueue {
Queue<T> array[PQSIZE];
};
void initialize(PriorityQueue<T>&);
void destroy(PriorityQueue<T>&);
int getSize(PriorityQueue<T>);
bool isEmpty(PriorityQueue<T>);
void push(PriorityQueue<T>&,T,int );
T pop(PriorityQueue<T>&);
And make the functions for a priority queue. I'm not sure what to do with how the struct declares a Queue<T> array[PQSIZE]. So basically I'm making an array of queues but i don't know how to push/pop to a queue within the array.
Sep 17, 2014 at 12:05am UTC
"this header file" is not valid C++. So nobody else here knows "what to do with how the".
Sep 17, 2014 at 12:30am UTC
Sorry, forgot to include the implementation of queue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
using namespace std;
const int SIZE = 5;
template <class T>
struct Queue {
T data[SIZE];
int tail;
};
template <class T>
void initialize(Queue<T>& Q) {
Q.tail = 0;
}
template <class T>
void destroy(Queue<T>& Q) {
Q.tail = 0;
}
template <class T>
int getSize(Queue<T> Q) {
return Q.tail;
}
template <class T>
bool isEmpty(Queue<T> Q) {
if (Q.tail == 0)
return true ;
else
return false ;
}
template <class T>
void push(Queue<T>& Q, T value) {
Q.data[Q.tail] = value;
Q.tail++;
}
template <class T>
T pop(Queue<T>& Q) {
T trash = Q.data[0];
for (int i = 0; i < Q.tail; i++) {
Q.data[i] = Q.data[i+1];
}
Q.tail--;
return trash;
}
Sep 17, 2014 at 12:38am UTC
This is what I'm using to push values into each queue:
1 2 3 4
template <class T>
void push(PriorityQueue<T>& Q,T value,int rank) {
push(Q.array[rank], value);
}
Now I can't figure out how to pop the values out of the highest priority queues.
Sep 17, 2014 at 12:53am UTC
I'm trying this for my pop function, getting the isEmpty function from the header file, but it keeps telling me "no match for operator==" on line 5 below.
1 2 3 4 5 6 7 8 9 10 11
template <class T>
T pop(PriorityQueue<T>& Q) {
int count = PQSIZE;
while (true ) {
if (isEmpty((Q.array[count]) == true ))
count--;
else
break ;
}
return pop(Q.array[count]);
}
Last edited on Sep 17, 2014 at 12:53am UTC
Sep 17, 2014 at 2:23am UTC
Your class is not copyable and you accidently neglected the ampersand in some of your declarations. You need to always pass by reference anyway.
Topic archived. No new replies allowed.