C ++分段故障链接列表元素添加

时间:2014-06-22 08:37:17

标签: c++ linked-list

我在用c ++编写链接列表实现时遇到了问题。每当我尝试添加元素时,我都会收到Segmentation Fault错误。

 #include <iostream>


    class node{
    public:
        int data;
        class node *next;
    };
    class LinkedList {
    public:
        node *head;
        LinkedList(int num);

        void add(int number);
        void display();
    };

    const int null = 0;
    LinkedList::LinkedList(int num) {
        // TODO Auto-generated constructor stub
        std::cout<<"Constructor Called"<<std::endl;
        head->data = num;
        head->next = null;
        std::cout<<"Constructor Call completed"<<std::endl;
    }



    void LinkedList::add(int num) {
        struct node *n=new node;
        n->data = num;
        n->next = null;
        struct node *current = head;
        while (current->next != null) {
            current = current->next;
        }
        current->next = n;
    }
    void LinkedList::display() {
        std::cout<<"Display"<<std::endl;
        struct node *current = head;

        while (current!= null) {
            std::cout << current->data << "->";
            current = current->next;
        }
        std::cout << "null"<<std::endl;
    }

    int main() {
        LinkedList l(1);
        l.display();
        l.add(2);
        l.display();
        return 0;
    }

请查看gdb调试日志:节点n未引用任何内存位置。所以它无法初始化。如果您需要任何进一步的信息,请告诉我。

提前致谢!

struct node *n=new node;
Constructor Called
Constructor Call completed

Breakpoint 1, main () at SampleCPP.cpp:60
60      l.display();
(gdb) s
LinkedList::display (this=0xbffff03c) at SampleCPP.cpp:48
48      std::cout
(gdb) n
51      while (current!= null) {
(gdb) n
52          std::cout data ";
(gdb) n
53          current = current->next;
(gdb) n
51      while (current!= null) {
(gdb) n
55      std::cout null
56  }
(gdb) n
main () at SampleCPP.cpp:61
61      l.add(2);
(gdb) s
LinkedList::add (this=0xbffff03c, num=2) at SampleCPP.cpp:38
38      struct node *n=new node;
(gdb) print n
$2 = (node *) 0x0
(gdb) 

4 个答案:

答案 0 :(得分:4)

您永远不会为head分配内存。这是你的问题。

在构造函数中分配它:

LinkedList::LinkedList(int num) {
    std::cout<<"Constructor Called"<<std::endl;
    head = new node; // This is missing!
    head->data = num;
    head->next = null;
    std::cout<<"Constructor Call completed"<<std::endl;
}

并且在程序完成之前释放所有内存也是一件好事。

答案 1 :(得分:1)

你没有为头部分配内存;它应该是这样的

LinkedList::LinkedList(int num) {
    // TODO Auto-generated constructor stub
    head=new node();
    std::cout<<"Constructor Called"<<std::endl;
    head->data = num;
    head->next = null;
    std::cout<<"Constructor Call completed"<<std::endl;
}

答案 2 :(得分:0)

经过少量修改,

class node{
public:
    int data;
    class node *next;
    node(int num): data(num), next(NULL){} 
};

并且

LinkedList::LinkedList(int num): head(new node(num))  {
    // TODO Auto-generated constructor stub
    std::cout<<"Constructor Called"<<std::endl;
    //Do sth if you wish
    std::cout<<"Constructor Call completed"<<std::endl;
}

这消除了一些额外的复杂性。使用新构造函数更改用于实例化节点的位置。

答案 3 :(得分:0)

我想建议以下实现:

#include <iostream>

using namespace std;


class node
{
private:
    int data;
    node* next;

public:
    // Because of that we store the pointer, default implementations of the
    // copying operations are not acceptable -- they can lead to memory leakage.
    node(const node&) = delete;
    node& operator =(const node&) = delete;

    node(int d, node* prev = nullptr) :
        data(d), next(nullptr)
    {
        if(prev)
        {
            // Free memory before assigning the pointer.
            delete prev->next;
            prev->next = this;
        }
    }
    ~node()
    {
        // Deletes up to the end of the subchain.
        delete next;
    }

    inline node* getNext() const
    {
        return next;
    }
    inline operator int() const
    {
        return data;
    }
};

class LinkedList
{
private:
    node* head;
    node* curr;

public:
    LinkedList(int first_entry) :
        head(new node(first_entry)), curr(head)
    {}
    ~LinkedList()
    {
        delete head;
    }

    void add(int entry)
    {
        curr = new node(entry, curr);
    }
    void display() const
    {
        for(node* i = head; i; i = i->getNext())
            cout << (int)*i << "->";
        cout << "null" << endl;
    }
};


int main(int argc, char* argv[])
{
    LinkedList l(1);
    l.display();
    l.add(2);
    l.display();

    return EXIT_SUCCESS;
}

在这种方法中,额外关注内存管理。它也更密集地使用 OOP。