Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
普通链表 我决定实践我的链表知识,决定用C++来创建一个。p>_C++_Linked List - Fatal编程技术网

普通链表 我决定实践我的链表知识,决定用C++来创建一个。p>

普通链表 我决定实践我的链表知识,决定用C++来创建一个。p>,c++,linked-list,C++,Linked List,我在两个不同的在线编译器上运行了这段代码——一个运行正常,另一个给我一个segfault。我无法找出代码中的问题所在,不知道您是否可以帮助我 #include <iostream> using namespace std; struct Node { int val; Node *next; Node(int val){ this->val = val; this->next = NULL; } };

我在两个不同的在线编译器上运行了这段代码——一个运行正常,另一个给我一个segfault。我无法找出代码中的问题所在,不知道您是否可以帮助我

#include <iostream>

using namespace std;

struct Node {
    int val;
    Node *next;
    Node(int val){
        this->val = val;
        this->next = NULL;
    }
};

class LinkedList {
public:
    Node *head;

    void insertAtHead(Node *temp)
    {
        if (head == NULL) 
        {
            head = temp;
        }
        else
        {
            temp->next = head;
            head = temp;
        }
    }

    void printList()
    {
        Node *temp = head;
        while (temp != NULL)
        {
            cout << temp->val << endl;
            temp = temp->next;
        }
    }

    void insertAtBack(Node *temp)
    {
        if (head == NULL)
        {
            head = temp;
            return;
        }

        Node *current = head;
        while (current->next != NULL){
            current = current->next;
        }
        current->next = temp;
    }

    void deleteNode(Node *temp)
    {
        if (head == NULL)
        {
            cout << "Empty List";
            return;
        }
        if (head->val == temp->val)
        {
            head = head->next;
            return;
        }

        Node *current = head;
        while (current->next != NULL)
        {
            if (current->next->val == temp->val)
            {
                current->next = current->next->next;
                return;
            }
            current = current->next;
        }
    }
};

int main()
{
    Node *temp = new Node(10);
    Node *temp2 = new Node(4);
    Node *temp3 = new Node(17);
    Node *temp4 = new Node(22);
    Node *temp5 = new Node(1);
    LinkedList x;
    x.insertAtHead(temp);
    x.insertAtHead(temp2);
    x.insertAtBack(temp3);
    // x.insertAtBack(temp4);
    // x.insertAtBack(temp5);
    // x.deleteNode(temp);
    x.printList();

    return 0;
}
我遇到的问题是当我使用insertback方法时。这给了我一个错误,但我看不出逻辑有什么问题。这很直截了当。insertAtFront方法可以工作,但一旦调用insertAtBack,我的代码就会失败。

请确保将Node*head初始化为NULL

插入值为10的temp后,temp->next值变为未定义值,因为Node*head是未定义值。

确保将Node*head初始化为NULL

插入值为10的temp后,temp->next值变为未定义值,因为Node*head是未定义值。

您的LinkedList类没有初始化其head成员。您需要添加一个构造函数来将head初始化为NULL

此外,该类正在泄漏内存,因为当LinkedList实例被销毁时,没有析构函数来释放节点,并且deleteNode也不会释放要删除的节点

尝试类似以下内容:

#include <iostream>

using namespace std;

struct Node
{
    int val;
    Node *next;

    Node(int val) : val(val), next(NULL) { }
};

class LinkedList
{
private:
    Node *head;

    // if you are NOT using C++11 or later, add these
    // until you are reading to tackle copy semantics!
    /*
    LinkedList(const LinkedList &);
    LinkedList& operator=(const LinkedList &);
    */

public:
    LinkedList() : head(NULL) {} // <-- add this!

    ~LinkedList() // <-- add this!
    {
        Node *current = head;
        while (current)
        {
             Node *next = current->next;
             delete current;
             current = next;
        }
    }

    void insertAtHead(Node *temp)
    {
        if (!head) 
        {
            head = temp;
        }
        else
        {
            temp->next = head;
            head = temp;
        }
    }

    void printList()
    {
        Node *current = head;
        while (current)
        {
            cout << current->val << endl;
            current = current->next;
        }
    }

    void insertAtBack(Node *temp)
    {
        if (!head)
        {
            head = temp;
            return;
        }

        Node *current = head;
        while (current->next) {
            current = current->next;
        }
        current->next = temp;
    }

    void deleteNode(Node *temp)
    {
        if (!head)
        {
            cout << "Empty List";
            return;
        }

        if (head == temp)
        {
            head = temp->next;
            delete temp;
            return;
        }

        Node *current = head;
        while (current->next)
        {
            if (current->next == temp)
            {
                current->next = temp->next;
                delete temp;
                return;
            }
            current = current->next;
        }
    }

    // if you ARE using C++11 or later, add these until
    // you are reading to tackle copy and move semantics!
    /*
    LinkedList(const LinkedList &) = delete;
    LinkedList(LinkedList &&) = delete;
    LinkedList& operator=(const LinkedList &) = delete;
    LinkedList& operator=(LinkedList &&) = delete;
    */
};

int main()
{
    Node *temp = new Node(10);
    Node *temp2 = new Node(4);
    Node *temp3 = new Node(17);
    Node *temp4 = new Node(22);
    Node *temp5 = new Node(1);

    LinkedList x;
    x.insertAtHead(temp);
    x.insertAtHead(temp2);
    x.insertAtBack(temp3);
    // x.insertAtBack(temp4);
    // x.insertAtBack(temp5);
    // x.deleteNode(temp);

    x.printList();

    return 0;
}
然后可以进一步简化:

#include <iostream>

using namespace std;

struct Node
{
    int val;
    Node *next;

    Node(int val, Node *next = NULL) : val(val), next(next) { }
};

class LinkedList
{
private:
    Node *head;

    // if you are NOT using C++11 or later, add these
    // until you are reading to tackle copy semantics!
    /*
    LinkedList(const LinkedList &);
    LinkedList& operator=(const LinkedList &);
    */

public:
    LinkedList() : head(NULL) {} // <-- add this!

    ~LinkedList() // <-- add this!
    {
        Node *current = head;
        while (current)
        {
             Node *next = current->next;
             delete current;
             current = next;
        }
    }

    Node* insertAtHead(int value)
    {
        Node *temp = new Node(value, head);
        if (!head) 
            head = temp;
        return temp;
    }

    void printList()
    {
        Node *current = head;
        while (current)
        {
            cout << current->val << endl;
            current = current->next;
        }
    }

    Node* insertAtBack(int value)
    {
        Node **current = &head;
        while (*current)
            current = &((*current)->next);
        *current = new Node(value);
        return *current;
    }

    /*
    void deleteNode(Node *temp)
    {
        Node *current = head, *previous = NULL;
        while (current)
        {
            if (current == temp)
            {
                if (previous)
                    previous->next = temp->next;
                if (head == temp)
                    head = temp->next;
                delete temp;
                return true;
            }
            previous = current;
            current = current->next;
        }
        cout << "Not found" << endl;
        return false;
    }
    */

    bool deleteValue(int value)
    {
        Node *current = head, *previous = NULL;
        while (current)
        {
            if (current->val == value)
            {
                if (previous)
                    previous->next = temp->next;
                if (head == temp)
                    head = temp->next;
                delete temp;
                return true;
            }
            previous = current;
            current = current->next;
        }
        cout << "Not found" << endl;
        return false;
    }

    // if you ARE using C++11 or later, add these until
    // you are reading to tackle copy and move semantics!
    /*
    LinkedList(const LinkedList &) = delete;
    LinkedList(LinkedList &&) = delete;
    LinkedList& operator=(const LinkedList &) = delete;
    LinkedList& operator=(LinkedList &&) = delete;
    */
};

int main()
{
    LinkedList x;
    x.insertAtHead(10);
    x.insertAtHead(4);
    x.insertAtBack(17);
    // x.insertAtBack(22);
    // x.insertAtBack(1);
    // x.deleteValue(10);

    x.printList();

    return 0;
}
您的LinkedList类未初始化其头成员。您需要添加一个构造函数来将head初始化为NULL

此外,该类正在泄漏内存,因为当LinkedList实例被销毁时,没有析构函数来释放节点,并且deleteNode也不会释放要删除的节点

尝试类似以下内容:

#include <iostream>

using namespace std;

struct Node
{
    int val;
    Node *next;

    Node(int val) : val(val), next(NULL) { }
};

class LinkedList
{
private:
    Node *head;

    // if you are NOT using C++11 or later, add these
    // until you are reading to tackle copy semantics!
    /*
    LinkedList(const LinkedList &);
    LinkedList& operator=(const LinkedList &);
    */

public:
    LinkedList() : head(NULL) {} // <-- add this!

    ~LinkedList() // <-- add this!
    {
        Node *current = head;
        while (current)
        {
             Node *next = current->next;
             delete current;
             current = next;
        }
    }

    void insertAtHead(Node *temp)
    {
        if (!head) 
        {
            head = temp;
        }
        else
        {
            temp->next = head;
            head = temp;
        }
    }

    void printList()
    {
        Node *current = head;
        while (current)
        {
            cout << current->val << endl;
            current = current->next;
        }
    }

    void insertAtBack(Node *temp)
    {
        if (!head)
        {
            head = temp;
            return;
        }

        Node *current = head;
        while (current->next) {
            current = current->next;
        }
        current->next = temp;
    }

    void deleteNode(Node *temp)
    {
        if (!head)
        {
            cout << "Empty List";
            return;
        }

        if (head == temp)
        {
            head = temp->next;
            delete temp;
            return;
        }

        Node *current = head;
        while (current->next)
        {
            if (current->next == temp)
            {
                current->next = temp->next;
                delete temp;
                return;
            }
            current = current->next;
        }
    }

    // if you ARE using C++11 or later, add these until
    // you are reading to tackle copy and move semantics!
    /*
    LinkedList(const LinkedList &) = delete;
    LinkedList(LinkedList &&) = delete;
    LinkedList& operator=(const LinkedList &) = delete;
    LinkedList& operator=(LinkedList &&) = delete;
    */
};

int main()
{
    Node *temp = new Node(10);
    Node *temp2 = new Node(4);
    Node *temp3 = new Node(17);
    Node *temp4 = new Node(22);
    Node *temp5 = new Node(1);

    LinkedList x;
    x.insertAtHead(temp);
    x.insertAtHead(temp2);
    x.insertAtBack(temp3);
    // x.insertAtBack(temp4);
    // x.insertAtBack(temp5);
    // x.deleteNode(temp);

    x.printList();

    return 0;
}
然后可以进一步简化:

#include <iostream>

using namespace std;

struct Node
{
    int val;
    Node *next;

    Node(int val, Node *next = NULL) : val(val), next(next) { }
};

class LinkedList
{
private:
    Node *head;

    // if you are NOT using C++11 or later, add these
    // until you are reading to tackle copy semantics!
    /*
    LinkedList(const LinkedList &);
    LinkedList& operator=(const LinkedList &);
    */

public:
    LinkedList() : head(NULL) {} // <-- add this!

    ~LinkedList() // <-- add this!
    {
        Node *current = head;
        while (current)
        {
             Node *next = current->next;
             delete current;
             current = next;
        }
    }

    Node* insertAtHead(int value)
    {
        Node *temp = new Node(value, head);
        if (!head) 
            head = temp;
        return temp;
    }

    void printList()
    {
        Node *current = head;
        while (current)
        {
            cout << current->val << endl;
            current = current->next;
        }
    }

    Node* insertAtBack(int value)
    {
        Node **current = &head;
        while (*current)
            current = &((*current)->next);
        *current = new Node(value);
        return *current;
    }

    /*
    void deleteNode(Node *temp)
    {
        Node *current = head, *previous = NULL;
        while (current)
        {
            if (current == temp)
            {
                if (previous)
                    previous->next = temp->next;
                if (head == temp)
                    head = temp->next;
                delete temp;
                return true;
            }
            previous = current;
            current = current->next;
        }
        cout << "Not found" << endl;
        return false;
    }
    */

    bool deleteValue(int value)
    {
        Node *current = head, *previous = NULL;
        while (current)
        {
            if (current->val == value)
            {
                if (previous)
                    previous->next = temp->next;
                if (head == temp)
                    head = temp->next;
                delete temp;
                return true;
            }
            previous = current;
            current = current->next;
        }
        cout << "Not found" << endl;
        return false;
    }

    // if you ARE using C++11 or later, add these until
    // you are reading to tackle copy and move semantics!
    /*
    LinkedList(const LinkedList &) = delete;
    LinkedList(LinkedList &&) = delete;
    LinkedList& operator=(const LinkedList &) = delete;
    LinkedList& operator=(LinkedList &&) = delete;
    */
};

int main()
{
    LinkedList x;
    x.insertAtHead(10);
    x.insertAtHead(4);
    x.insertAtBack(17);
    // x.insertAtBack(22);
    // x.insertAtBack(1);
    // x.deleteValue(10);

    x.printList();

    return 0;
}

改变节点*头部;到节点*head=NULL;我决定实践我的链表知识,决定用C++来创建一个!哦,为什么???为什么不使用C++标准库中的列表类来生成有用的程序——通过生成这样的程序,你将学到更多的东西,而不是产生另一个可怕的链表实现。我强烈建议用调试器来跳过代码。我决定实践我的链表知识,并决定在C++中创建一个。问题在于,您需要充分了解C++来正确地实现链表。您需要知道如何正确管理内存,而不仅仅是在教科书中找到的链表的基础知识。如果是Java或C,也许,但是C++有适当的内存管理的额外障碍,3或5的规则等等。@ NeilButterworth——我认为在编写实用工具类中作为一个ExcCijava来做这件事是完全好的。最终,OP将理解为什么会出现泛型、智能指针等问题,。。。。我希望存在。对于真正的链表,OP当然应该使用std::list。也许有点野心,对于第一个C++程序改变节点*HOAD;到节点*head=NULL;我决定实践我的链表知识,决定用C++来创建一个!哦,为什么???为什么不使用C++标准库中的列表类来生成有用的程序——通过生成这样的程序,你将学到更多的东西,而不是产生另一个可怕的链表实现。我强烈建议用调试器来跳过代码。我决定实践我的链表知识,并决定在C++中创建一个。问题在于,您需要充分了解C++来正确地实现链表。您需要知道如何正确管理内存,而不仅仅是在教科书中找到的链表的基础知识。如果是Java或C,也许,但是C++有适当的内存管理的额外障碍,3或5的规则等等。@ NeilButterworth——我认为在编写实用工具类中作为一个ExcCijava来做这件事是完全好的。最终,OP将理解为什么会出现泛型、智能指针等问题,。。。。我希望存在。对于真正的链表,OP当然应该使用std::list。也许有点野心,第一个C++程序