面向分段错误的链表程序 我编写了一个C++程序来实现链表。在编译时,它不会给出任何错误,但在输出窗口中它变为空白,程序以

面向分段错误的链表程序 我编写了一个C++程序来实现链表。在编译时,它不会给出任何错误,但在输出窗口中它变为空白,程序以,c++,C++,list1.exe已被删除 遇到问题,需要关闭 调试器响应:程序收到信号SIGSEGV,分段错误 可能是因为内存泄漏,但我无法找出确切的错误,我们如何修复它。请问程序中出现了什么问题,应该修复什么 下面是代码 //Program to implement linked list #include <iostream> #include <cstdlib> using namespace std; class Node { int

list1.exe已被删除 遇到问题,需要关闭

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

可能是因为内存泄漏,但我无法找出确切的错误,我们如何修复它。请问程序中出现了什么问题,应该修复什么

下面是代码

  //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;
  }
//实现链表的程序
#包括
#包括
使用名称空间std;
类节点
{
int数据;
节点*下一步;
公众:
节点(){}
int getdata(){return data;}
void setdata(inta){data=a;}
void setnext(Node*c){next=c;}
Node*getnext(){return next;}
};
类链接列表
{
节点*头;
公众:
linkedlist(){head=NULL;}
作废印刷品();
无效推回(int数据);
};
void linkedlist::push_back(int数据)
{
Node*newnode=newnode();
if(newnode!=NULL)
{
新建节点->设置数据(数据);
新建节点->设置下一步(空);
}
节点*ptr=头部;
如果(ptr==NULL)
{head=newnode;}
而((ptr->getnext())!=NULL)
{
ptr=ptr->getnext();
}
ptr->setnext(新节点);
}
作废链接列表::打印()
{
节点*ptr=头部;
如果(ptr==NULL)

{cout在下一行:
while((ptr->getnext())!=NULL)
ptr为NULL

在下一行:
while((ptr->getnext())!=NULL)
ptr为NULL

推回
代码不正确,我已经看到代码的其他部分可以改进:

#include <iostream>
#include<cstdlib>

using namespace std;

class Node
{
      int data;
      Node * next;

   public:
      Node(int d = 0) : data(d), next(NULL) {}

      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(data);

   if(head == NULL)
   {
      head = newnode;
   }
   else
   {
      Node* last = head;
      while(last->getnext() != NULL)
         last = last->getnext();
      last->setnext(newnode);
   }
}

void linkedlist::print()
{
   Node* ptr = head;
   if(!ptr)
   {
      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;
}
#包括
#包括
使用名称空间std;
类节点
{
int数据;
节点*下一步;
公众:
节点(intd=0):数据(d),下一个(NULL){
int getdata(){return data;}
void setdata(inta){data=a;}
void setnext(Node*c){next=c;}
Node*getnext(){return next;}
};
类链接列表
{
节点*头;
公众:
linkedlist():头(空){}
作废印刷品();
无效推回(int数据);
};
void linkedlist::push_back(int数据)
{
Node*newnode=新节点(数据);
if(head==NULL)
{
头=新节点;
}
其他的
{
节点*最后一个=头部;
while(last->getnext()!=NULL)
last=last->getnext();
最后->设置下一步(新节点);
}
}
作废链接列表::打印()
{
节点*ptr=头部;
如果(!ptr)
{

coutpush_back
代码不正确,我已经看到您代码的其他部分可以改进:

#include <iostream>
#include<cstdlib>

using namespace std;

class Node
{
      int data;
      Node * next;

   public:
      Node(int d = 0) : data(d), next(NULL) {}

      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(data);

   if(head == NULL)
   {
      head = newnode;
   }
   else
   {
      Node* last = head;
      while(last->getnext() != NULL)
         last = last->getnext();
      last->setnext(newnode);
   }
}

void linkedlist::print()
{
   Node* ptr = head;
   if(!ptr)
   {
      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;
}
#包括
#包括
使用名称空间std;
类节点
{
int数据;
节点*下一步;
公众:
节点(intd=0):数据(d),下一个(NULL){
int getdata(){return data;}
void setdata(inta){data=a;}
void setnext(Node*c){next=c;}
Node*getnext(){return next;}
};
类链接列表
{
节点*头;
公众:
linkedlist():头(空){}
作废印刷品();
无效推回(int数据);
};
void linkedlist::push_back(int数据)
{
Node*newnode=新节点(数据);
if(head==NULL)
{
头=新节点;
}
其他的
{
节点*最后一个=头部;
while(last->getnext()!=NULL)
last=last->getnext();
最后->设置下一步(新节点);
}
}
作废链接列表::打印()
{
节点*ptr=头部;
如果(!ptr)
{

cout主要问题是:

if(ptr==NULL) {head=newnode;}
while ((ptr->getnext())!=NULL)
{
    ptr=ptr->getnext();
}
ptr->setnext(newnode);
if(ptr==NULL)
部分中可能有一个
返回;
;目前,它设置
head=newnode
,但随后继续尝试访问
ptr->getnext()
,这会导致segfault

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

出于您的兴趣,以下是您的代码:

  • 格式很好
  • 清理后不使用
    使用命名空间std;
    (请参阅)
  • 利用
  • 等等
享受吧

#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;
}
#包括
#包括
类节点{
int数据;
节点*下一步;
公众:
Node():下一个(NULL){}
int getdata()常量{
返回数据;
}
无效设置数据(int a){
数据=a;
}
节点*getnext()常量{
下一步返回;
}
void setnext(节点*c){
next=c;
}
};
类链接列表{
节点*头;
公众:
linkedlist():头(空){}
void print()常量{
节点*ptr=头部;
如果(ptr==NULL){
std::cout setdata(数据);
节点*ptr=头部;
如果(ptr==NULL){
头=新节点;
返回;
}
而((ptr->getnext())!=NULL){
ptr=ptr->getnext();
}
ptr->setnext(新节点);
}
};
int main(){
链接列表;
列表。推回(30);
列表。推回(35);
list.print();
返回0;
}

主要问题是:

if(ptr==NULL) {head=newnode;}
while ((ptr->getnext())!=NULL)
{
    ptr=ptr->getnext();
}
ptr->setnext(newnode);
if(ptr==NULL)
部分中可能有一个
返回;
;目前,它设置
head=newnode
,但随后继续尝试访问
ptr->getnext()
,这会导致segfault

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

出于您的兴趣,以下是您的代码:

  • 格式很好
  • 清理后不使用
    使用命名空间std;
    (请参阅)
  • 利用
  • 等等
享受吧

#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;
}
#包括
#包括
类节点{
int数据;
节点*下一步;
公众:
Node():下一个(NULL){}
int getdata()常量{
返回数据;
}
无效设置数据(int a){
数据=a;
}
节点*getnext()常量{