0

リンク リストを実装する 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;
  }
4

3 に答える 3

4

The main issue is here:

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

There's probably meant to be a return; in the if (ptr == NULL) part; as it stands, it sets head = newnode, but then continues to try to access ptr->getnext(), which causes the segfault.

Some answers have suggested setting ptr = head = newnode, but note that the bottom line is ptr->setnext(newnode)—this would cause head->getnext() == head. Infinite list!

For your interest, here's your code:

Enjoy!

#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;
}
于 2012-07-09T12:37:21.887 に答える
0

次の行:while ((ptr->getnext())!=NULL)ptrはNULLです

于 2012-07-09T12:30:38.110 に答える
0

コードが正しくありません。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;
}

まだまだ改善点はありますが…

于 2012-07-09T12:33:43.697 に答える