extractMin()
当我多次调用时,此代码会崩溃。我认为对你们中的一些人来说,问题出在函数上应该是显而易见的,因为我是指针新手,可能是一个明显的错误。所以你知道它是一个链表就足够了,除了函数应该使用<
运算符检索字典最小值,然后从链表中删除该值之外,无需详细说明。
string LinkedListPQueue::extractMin() {
if (this->isEmpty()) throw ErrorException("Empty queue.");
string front = LEX_HIGH;
cell *old;
for (int i = 0; i < this->size(); i++) {
if (this->head->value < front) {
front = this->head->value;
old = this->head;
}
old = this->head;
this->head = this->head->next;
}
logSize--;
delete old;
return front;
}
void LinkedListPQueue::enqueue(const string& elem) {
cell *newCell = new cell;
newCell->value = elem;
newCell->next = NULL;
if(this->isEmpty()) {
this->head = this->tail = newCell;
logSize++;
} else {
recurSort(newCell);
this->tail->next = newCell;
this->tail = newCell;
logSize++;
}
}