This code crashes when I call extractMin() more than once. I think it should be obvious to
some of you where the problem lies in the function since I am new to pointers and is probably an obvious bug. So it should be enough that you know it´s a linked list without going into detail other than the function is supposed to retrieve the lexicographical smallest value using the < operator, and then delete that value from the linked list.
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++;
}
}
You are modifying the head member in
extractMin(), which leaves the list broken.