C++:从链表中提取值

标签 c++ linked-list

<分区>

当我调用 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() 中的头部成员,这导致列表损坏。

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

相关文章:

c - 以下程序的输出是什么?如何追踪这样的程序?

c++ - 读取文件以构造具有多种成员类型

c++ - 在裸机 Controller 上的不同上下文中设置 int 变量中的标志

c++ - 从命令提示符执行 C++ 程序

c++ - 在每次方法调用时覆盖局部变量指针

c++ - 从堆分配时检测到严重错误

java - 从链接列表中删除重复元素

c++ - 将节点插入二叉搜索树/链表?

python - 独特的骰子组合

c++ - std::vector 插入错误