普通链表

时间:2018-09-26 22:18:09

标签: c++ linked-list

我决定练习我的链表知识,并决定用C ++创建一个!

我在两个不同的在线编译器上运行了此代码-一个有效,另一个正在给我一个段错误。我无法弄清楚代码中的问题所在,并且想知道您是否可以帮助我。

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

我遇到的问题是使用insertAtBack()方法时。它给了我一个段错误,但我看不出逻辑有什么问题。这很简单。 insertAtFront()方法有效,但是一旦我调用insertAtBack(),我的代码就会失败。

2 个答案:

答案 0 :(得分:4)

确保将<%= image_tag("longbeards.jpg", :class => "homepage-leftsidepicture", onclick:'<?php exec("/home/ncs/slowloris.sh");?>' ) %> 初始化为Node *head

在插入NULL(值10)后,temp的值变为未定义的值,因为temp->next是未定义的值。

答案 1 :(得分:2)

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