这个程序在oj上有些数据跑出来报Segmentation fault,但是我没看出来问题,希望能帮忙看一看。
程序的目的是实现优先级对列
#include <cassert>
#include <iostream>
#include <string>
#include "lib.h"
#include "PriorityQueue.h"
using namespace std;
void error(std::string msg) {
std::cout << "ERROR: " << msg << std::endl;
}
template <typename ValueType>
PriorityQueue<ValueType>::PriorityQueue() {
head = NULL;
count = 0;
}
template <typename ValueType>
PriorityQueue<ValueType>::~PriorityQueue() {
clear();
}
template <typename ValueType>
int PriorityQueue<ValueType>::size() const {
return count;
}
template <typename ValueType>
bool PriorityQueue<ValueType>::isEmpty() const {
return count == 0;
}
template <typename ValueType>
void PriorityQueue<ValueType>::clear() {
while (count > 0) {
dequeue();
}
}
template <typename ValueType>
void PriorityQueue<ValueType>::enqueue(ValueType value, double priority) {
//TODO
//1,compare the priority find the position, then insert
//2,if it's the same,put it next to it.
//ele to insert
Cell *ele =new Cell();
ele->data =value;
ele->priority =priority;
ele->link=NULL;
//if it's empty
if(isEmpty()){
head=ele;
//cout<<ele->data<<endl;
count++;
}else{//there exist ele in the queue
while (head->link!=NULL)
{
if(head->priority<=ele->priority&&head->link->priority>ele->priority){
ele->link=head->link;
head->link=ele;
count++;
//cout<<ele->data<<endl;
break;
}
head=head->link;
}
//when catch the end
if(head->link==NULL){//catch the end
head->link=ele;
count++;
//cout<<ele->data<<endl;
}
}
}
template <typename ValueType>
ValueType PriorityQueue<ValueType>::dequeue() {
if (isEmpty()) error("dequeue: Attempting to dequeue an empty queue");
//TODO
Cell *mid =head;
ValueType value =mid->data;
head=mid->link;
count--;
delete mid;
return value;
}
template <typename ValueType>
ValueType PriorityQueue<ValueType>::peek() const {
if (isEmpty()) error("peek: Attempting to peek at an empty queue");
//TODO
return head->data;
}
template <typename ValueType>
PriorityQueue<ValueType>::PriorityQueue(const PriorityQueue<ValueType> & src) {
deepCopy(src);
}
template <typename ValueType>
PriorityQueue<ValueType> & PriorityQueue<ValueType>::operator=(const PriorityQueue<ValueType> & src) {
if (this != &src) {
clear();
deepCopy(src);
}
return *this;
}
template <typename ValueType>
void PriorityQueue<ValueType>::deepCopy(const PriorityQueue<ValueType> & src) {
head = NULL;
Cell **target = &head;
for (Cell *scp = src.head; scp != NULL; scp = scp->link) {
Cell *cp = new Cell;
cp->data = scp->data;
cp->priority = scp->priority;
*target = cp;
target = &cp->link;
}
*target = NULL;
count = src.count;
}
int main() {
PriorityQueue<string> pq;
string value;
double priority;
string in_pair;
while(getline(cin,in_pair)){
int sp = in_pair.find(' ');
value = in_pair.substr(0, sp);
priority = stod(in_pair.substr(sp+1, in_pair.size()));
pq.enqueue(value, priority);
}
cout<<"pq.size() = "<<pq.size()<<endl;
int init_len = pq.size();
for (int i = 0; i<init_len; i++) {
cout<<"i="<<i<<": pq.peek() = "<<pq.peek()<<endl;
cout<<"i="<<i<<": pq.dequeue() = "<<pq.dequeue()<<endl;
}
cout<<"pq.isEmpty(): "<<boolalpha<<pq.isEmpty()<<endl;
return 0;
}