c++ - 链表程序面临段错误

标签 c++

我正在编写一个 C++ 程序来实现链表。在编译时它没有给出任何错误,但在输出窗口中它变为空白并且程序以

结束

list1.exe has encountered a problem and needs to close.

调试器响应:程序接收到信号 SIGSEGV,段错误。

也许是因为内存泄漏,但我无法找出确切的错误以及我们如何修复它。请问 prog 有什么问题,应该修复什么?

下面是代码

  //Program to implement linked list

  #include <iostream>
  #include <cstdlib>

  using namespace std;

  class Node
  {
      int data;
      Node * next;

   public:
      Node (){}
      int getdata(){return data ;}
      void setdata(int a){data=a;}
      void setnext(Node* c){next=c;}
      Node* getnext(){return next;}
  };

  class linkedlist
  {
      Node* head;

  public:
      linkedlist(){head=NULL;}
      void print ();
      void push_back(int data);
  };

  void linkedlist::push_back(int data)
  {
      Node* newnode= new Node();
      if(newnode!=NULL)
      {
          newnode->setdata(data);
          newnode->setnext(NULL);
      }
      Node* ptr= head;

      if(ptr==NULL) 
          {head=newnode;}
      while ((ptr->getnext())!=NULL)
      {
          ptr=ptr->getnext();
      }
      ptr->setnext(newnode);
  }

  void linkedlist::print()
  {
      Node* ptr=head;
      if(ptr==NULL)
          {cout<<"null"; return;}

      while(ptr!=NULL)
      {
          cout<<(ptr->getdata())<<" ";
          ptr=ptr->getnext();
      }
  }

  int main()
  {
     linkedlist list;
      list.push_back(30);
      list.push_back(35);
      list.print();
      return 0;
  }

最佳答案

主要问题在这里:

if(ptr==NULL) {head=newnode;}
while ((ptr->getnext())!=NULL)
{
    ptr=ptr->getnext();
}
ptr->setnext(newnode);

if (ptr == NULL) 部分可能有一个return;;目前,它设置了 head = newnode,但随后继续尝试访问 ptr->getnext(),这导致了段错误。

一些答案​​建议设置ptr = head = newnode,但请注意底线是ptr->setnext(newnode)——这会导致head ->getnext() == 头。无限列表!

为了您的兴趣,这是您的代码:

尽情享受吧!

#include <iostream>
#include <stdexcept>

class Node {
    int data;
    Node *next;

public:
    Node(): next(NULL) {}

    int getdata() const {
        return data;
    }

    void setdata(int a) {
        data = a;
    }

    Node *getnext() const {
        return next;
    }

    void setnext(Node *c) {
        next = c;
    }
};

class linkedlist {
    Node* head;

public:
    linkedlist(): head(NULL) {} 

    void print() const {
        Node *ptr = head;

        if (ptr == NULL) {
            std::cout << "null";
            return;
        }

        while (ptr != NULL) {
            std::cout << ptr->getdata() << " ";
            ptr = ptr->getnext();
        }
    }

    void push_back(int data) {
        Node *newnode = new Node();

        if (newnode == NULL) {
            throw std::runtime_error("out of memory!");
        }

        newnode->setdata(data);

        Node *ptr = head;

        if (ptr == NULL) {
            head = newnode;
            return;
        }

        while ((ptr->getnext()) != NULL) {
            ptr = ptr->getnext();
        }

        ptr->setnext(newnode);
    }
};

int main() {
    linkedlist list;
    list.push_back(30);
    list.push_back(35);
    list.print();
    return 0;
}

关于c++ - 链表程序面临段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394985/

相关文章:

c++ - 为什么不将字符串的第一个输入分配给 std::string 对象?

C++:实现智能指针

c++ - 将 CUDA 设备交错数组转换为元组以进行 vector 运算

c++ - 使用 Autotools 构建 CUDA 和 C++

c++ - 是否有单击组中任何按钮的事件?

c++ - 没有删除运算符的指针类型

C++ char序列函数总是返回空字符串

c++ - 如何使用函数对象作为回调来调用对象中的成员函数(处理程序)?

c++ - 如何用移动语义重载一元算术

c# - 结构 - 初始化和写入文件