【问题标题】:C++ Linked List Simple Program CrashingC++ 链表简单程序崩溃
【发布时间】:2017-11-21 01:32:43
【问题描述】:

我创建了一个只有插入节点功能和打印功能的链接列表,但它不起作用。

#ifndef LIST_H_
#define LIST_H_
#include <iostream>
using namespace std;

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

class List{

private:
    Node* head;

public:
    List(){
        head = NULL;
    }

    void insertEnd(int d){
        Node* newNode = new Node;
        newNode->next = NULL;
        newNode->data = d;

        if (head == NULL){
            head = newNode;
            return;
        }

        Node* cu = head;

        while (cu != NULL)
            cu = cu->next;
        cu->next = newNode;
    }

    void printList(){
        Node* temp = new Node;
        temp = head;
        while (temp != NULL){
            cout << temp->data << ", ";
            temp = temp->next;
        }
    }

};

还有我的主要功能:

#include <iostream>
#include "List.h"
using namespace std;

int main(){

List list1;

list1.insertEnd(1);
list1.insertEnd(2);
list1.insertEnd(3);

//list1.printList();

return 0;

}

如果我只插入一个节点,这个程序就可以工作,但是如果我做任何其他事情,它就会崩溃并且不会给我任何错误指示或任何东西。

我已经在几个网站上检查了我的指针是否正确,我认为是正确的,但是这里出了什么问题...?

编辑:修复了问题...在while循环中应该是

while (cu->next != NULL)

【问题讨论】:

  • 它肯定会给你一个错误。如果您通过bat 运行此程序,请在末尾添加pause,以便您阅读错误。
  • 例如Node* cu = new Node; cu = head; - 认为这是存在意义吗?
  • insertEnd, printList() - 完全错误。 Node* temp = new Node; temp = head;这是c++?
  • 如果您无法自己找出发生了什么,您可能需要使用调试器。仅供参考 cu->next = newNode 将始终崩溃,因为您的 while 循环没有检查正确的条件。它应该检查 while(cu->next != null)。您当前的实现在一定程度上确保您的指针在退出 while 循环之前是一个 nullptr...
  • cu-&gt;next = newNode; after while (cu != NULL) - 所以 cu - 这里总是 0

标签: c++ linked-list append singly-linked-list


【解决方案1】:

您的 while 循环不正确。将其从 cu 更改为 cu-&gt;next

while (cu->next != NULL)

【讨论】:

    【解决方案2】:
    void insertEnd(int d){ 
            Node* newNode = new Node; 
            newNode->next = NULL; 
            newNode->data = d; 
    
            if (head == NULL){ 
                head = newNode; 
                return; 
            } 
    
            Node* cu = head; 
    
            while (cu->next != NULL) 
                cu = cu->next; 
            cu->next = newNode; 
    }
    

    这个函数可以解决问题。你有几个相对简单的问题。首先,您试图制作 head 的副本以遍历您的列表。您不是将其分配给虚拟指针,而是分配新内存,将新内存分配给虚拟指针,然后将头指针分配给该虚拟指针。这将造成内存泄漏,因为如果您忘记了它,您将永远无法删除该内存。我改变了这个:

    Node* cu = new Node;
    cu = head
    

    到这里:

    Node* cu = head;
    

    其次,当您在 while 循环中检查 cu 是否不为空时,就会出现分段错误。您在循环中将 cu 设置为 cu->next,然后检查 cu 是否为空。如果 cu 为空,则将 cu->next 分配给新节点。您的空指针不引用任何内存,因此尝试引用其成员会给您带来段错误。您想访问链接列表中最后一个可能的有效指针。为此,您检查 cu->next 是否为空。我改变了这个:

    while (cu != NULL)
                cu = cu->next;
    

    到这里:

    while (cu->next != NULL) 
                cu = cu->next;
    

    【讨论】:

      【解决方案3】:

      函数insertEnd 错误。

      在这个循环之后

          while (cu != NULL)
              cu = cu->next;
      

      指针cv 等于NULL。结果如下语句

          cu->next = newNode;
      

      导致未定义的行为。

      将节点附加到列表的最简单方法如下

      void insertEnd( int d )
      {
          Node **last = &head;
      
          while ( *last != nullptr ) last = &( *last )->next;
      
          *last = new Node { d, nullptr };
      }
      

      函数只有三行。:)

      考虑到这个声明

          Node* temp = new Node;
      

      在函数printList 中没有意义,是内存泄漏的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多