【问题标题】:Linked list program facing segmentation fault链表程序面临分段错误
【发布时间】:2012-07-09 22:03:25
【问题描述】:

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

结束

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;
  }

【问题讨论】:

  • 请以可读的格式格式化您的代码!
  • 它在哪一行中断?或者至少是哪种方法?
  • 你用过调试器吗?不?为什么不呢?
  • hi luchian..我很抱歉,实际上我是新手..你能告诉我应该如何使用调试器..我正在 COdeblocks IDE 中开发它...

标签: c++


【解决方案1】:

主要问题在这里:

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

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

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

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

享受吧!

#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;
}

【讨论】:

  • +1:因为你在回答一个写得很糟糕的问题时付出了很多努力。
  • @len: 非常感谢!是的,你是对的!我忘了放一个退货声明..但我没有改变任何其他东西..prog 工作正常..grt man!!!
【解决方案2】:

在以下行中:while ((ptr-&gt;getnext())!=NULL) ptr 为 NULL

【讨论】:

  • @Len:请再提供一个帮助...如果我将来也想使用这个 prog...比如制作一个头文件,然后在将来的 progs 中包含该文件..你能写吗并告诉我为实现这一目标需要采取的步骤。非常感谢!
【解决方案3】:

push_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;
}

还有一些地方需要改进……

【讨论】:

  • 你试过了吗?与@Razvan 的答案一样,当您将head-&gt;next 与自身联系在一起时,您的答案将导致无限循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
相关资源
最近更新 更多