This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
当我多次调用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++;
    }
}

最佳答案

您正在修改extractMin()中的head成员,这会使列表损坏。

关于c++ - C++:从链接列表中提取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14297105/

10-11 22:11
查看更多