Okay here is a quick implemenation I wrote up using a vector. It would have been nicer if vector had push_front/pop_front but for some reason they only have push/pop_back. My only concern is in my pop() method. I'm not sure if erase resizes the vector or not. Let me know what you think or if you see any glaring problems. thanks
InsQueue.h
Code:
#include <vector>
#include <string>
class Instruction{
public:
std::string ins_str;
int priority_level;
};
class InsQueue{
public:
void push(Instruction i);
void pop();
Instruction top();
bool empty();
private:
std::vector<Instruction> ins_vect;
};
InsQueue.cpp
Code:
#include "InsQueue.h"
void InsQueue::push(Instruction i){
for(std::vector<Instruction>::iterator it = ins_vect.begin(); it != ins_vect.end(); ++it){
if(i.priority_level > it->priority_level){
ins_vect.insert(it, i);
return;
}
}
ins_vect.push_back(i);
}
void InsQueue::pop(){
if(!ins_vect.empty())
ins_vect.erase(ins_vect.begin());
}
Instruction InsQueue::top(){
return ins_vect.front();
}
bool InsQueue::empty(){
return ins_vect.empty();
}