C++链表删除错误的项目

标签 c++ linked-list

<分区>

我正在尝试复习数据结构并实现一个基本的链表。当我运行这段代码时,我得到以下输出:

3 4 6

1 was found and deleted 4 was found and deleted

3

4 应该删除,但显然 1 不应该删除,我想知道我的代码/逻辑中的错误在哪里。

在此先感谢您的帮助。

#include <iostream>

using namespace std;

class List {

private:

    struct node {
        int data;
        node * next;
    };

    node * head;
    node * curr;
    node * temp;

public:

    List();

    void addNode(int newData);
    void deleteNode(int delData);
    void printList();

};

int main() {


    List test;

    test.addNode(3);
    test.addNode(4);
    test.addNode(6);

    test.printList();

    cout << endl << endl;

    test.deleteNode(1);
    test.deleteNode(4);

    cout << endl << endl;

    test.printList();



}

List::List(){

    head = NULL;
    curr = NULL;
    temp = NULL;
}

void List::addNode(int newData){

    node * n = new node;
    n->next = NULL;
    n->data = newData;

    if (head != NULL) { // List is intact

        curr = head; // if List is not empty, make curr equal to the head, and start at the beginning of the list.

        while(curr->next != NULL) { // Get to last item on the list
            curr = curr->next;
        }
        curr->next = n; // Use the last item, and point to the new node.
    }

    else { // empty list
        head = n; // new node is the head of the list.
    }
}

void List::deleteNode(int delData){

    node * n = new node;

    temp = head;
    curr = head;

    if (head != NULL) {
        while (curr->next != NULL && curr->data != delData) {
            temp = curr;
            curr = curr->next;
        }
        if (curr == NULL) {
            cout << delData << " was not found in the list\n";
            delete n;
        }
        else {
            n = curr;
            curr = curr->next;
            temp->next = curr;
            delete n;

            cout << delData << " was found and deleted\n";
        }
    }
}

void List::printList(){

    curr = head;

    while (curr != NULL) {
        cout << curr->data << endl;
        curr = curr->next;
    }

}

最佳答案

下一行分配一个新节点。

node * n = new node;

正如评论中已经指出的那样,尚不清楚为什么 deleteNode() 会这样做。 delete n 的后续行实际上是删除这个新节点,而不是列表中的节点之一。

我会尝试编写 deleteNode() 这样的东西:

void List::deleteNode(int delData) {
  // Empty list
  if (!head) return;

  // The first node is to be deleted
  if (head->data == delData) {
    node * old_head = head;
    head = head->next;
    delete old_head;
    return;
  }

  // A non-first node is to be deleted
  for (node * cur = head; cur; cur = cur->next) {
    if (cur->next && cur->next->data == delData) {
      node * del_node = cur->next;
      cur->next = cur->next->next;
      delete del_node;
      break;
    }
  }
}

关于C++链表删除错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44313357/

相关文章:

c - 段错误和链表的未知问题

c - 链表头未跨函数调用更新

c++ - 编译时绑定(bind) C++ 模板?

c++ - 为什么我不能构造一个带有大括号括起来的初始化列表的 gsl::span

c++ - 在 LLVM 中实现寄存器分配器

c - C语言链表结构中安全释放值的策略

Java链表lastIndexOf不工作

c - 如何使用链表初始化堆栈

c++ - 应该如何阅读堆缓冲区溢出错误消息?

c++ - 重新审视 switch 内部的对象实例化 (c++)