简体   繁体   English

C ++链表实现访问数据

[英]C++ Linked List Implementation accessing data

I have the custom Linked list implementation below: 我在下面有自定义的链接列表实现:

#include<iostream>

using namespace std;


struct Node {
    public:
    int data;
    struct Node *next = NULL;
};


class LinkedList {

    public:
        struct Node *head;
        int length = 0;

        void add(double item);
        void add_2(double item);
        void printLL();

};

void LinkedList::add_2(double item) {

    struct Node *node = new Node();
    node->data = item;
    node->next = head;

    head = node;
    length++;   
}

void LinkedList::add(double item) {
    struct Node node;
    node.data = item;
    node.next = head;
    head = &node;
    length++;
}

void LinkedList::printLL() {
    struct Node *cur_node;
    cur_node =  head;
    int i = 0;
    while (i<length) {
        cout << cur_node->data << " ";
        cur_node = cur_node->next;
        i++;
    }
}

It works fine with the driver code below: 可以与以下驱动程序代码配合使用:

int main() {
    LinkedList ll = LinkedList();
    ll.add(212);
    // cout << ll.head->data<<endl;
    ll.add_2(2123); 
    //ll.printLL();
    ll.add_2(2123);
    ll.printLL();
    return 0;
}

When I try to access the data in the driver code the output gets messed up: 当我尝试访问驱动程序代码中的数据时,输出变得混乱:

int main() {
    LinkedList ll = LinkedList();
    ll.add(212);
    cout << ll.head->data<<endl;
    ll.add_2(2123); 
    //ll.printLL();
    ll.add_2(2123);
    ll.printLL();
    return 0;
}

Result: 结果:

2123 2123 7339552 2123 2123 7339552

Expected: 预期:

2123 2123 212 2123 2123 212

Why is it that accessing the data from the driver code cout << ll.head->data<<endl changing the reference. 为什么从驱动程序代码cout << ll.head->data<<endl访问数据会更改引用。

void LinkedList::add(double item) {
    struct Node node;

node is a local variable with automatic storage. node是具有自动存储的局部变量。 Objects with automatic storage are destroyed automatically at the end of the scope (in this case, at the end of the function) 具有自动存储对象在范围的端部自动销毁(在这种情况下,在函数的末尾)

    head = &node;

You set the member head to point to the local variable. 您将成员head设置为指向局部变量。 After the function returns, the pointed node no longer exists and the pointer is left dangling. 函数返回后,指向的节点不再存在,并且指针悬空。 Indirecting through the danging pointer has undefined behaviour. 通过danging指针间接进行具有未定义的行为。

Solution: Get rid of the broken add function. 解决方案:摆脱损坏的add功能。 You already have a working add_2 function. 您已经有一个有效的add_2函数。 However, you do leak all the allocated memory. 但是,您确实泄漏了所有分配的内存。

Your implementation of linked list have problem with node variable usage. 您对链表的实现在节点变量使用方面存在问题。 It should be a pointer struct Node *node = new Node. 它应该是一个指针struct Node * node = new Node。 With this declaration and initialization, the node pointer will be in heap memory section and still live outside of method add_2. 通过此声明和初始化,节点指针将位于堆内存部分中,并且仍位于方法add_2之外。 with your implementation, it is in stack scope, and it will be invalid outside of method add_2. 在您的实现中,它在堆栈范围内,并且在方法add_2之外将无效。
For stopping memory leak, you need to implement the constructor and destructor of LinkedList. 为了阻止内存泄漏,您需要实现LinkedList的构造函数和析构函数。 With that way, when the LinkedList object is out of scope, its destructor will be called and free the dynamic memory that allocated by add_2 or add. 这样,当LinkedList对象超出范围时,将调用其析构函数并释放由add_2或add分配的动态内存。 You could add more method such as remove, empty,... to complete a LinkedList. 您可以添加更多方法,例如remove,empty,...以完成LinkedList。

#include <iostream>

using namespace std;

struct Node {
public:
    int data;
    struct Node *next = NULL;
};


class LinkedList {

public:
    LinkedList() : head(NULL), length(0)
    {

    }
    ~LinkedList()
    {
        struct Node *current = head;
        struct Node *prev = NULL;
        while (current != NULL)
        {
            prev = current;
            current = current->next;
            delete prev;
        }
        length = 0;
    }

    struct Node *head;
    int length = 0;

    void add(double item);
    void add_2(double item);
    void printLL();

};


// modified add is similar to add_2
void LinkedList::add(double item) {
    struct Node *node = new Node();
    node->data = item;
    node->next = head;

    head = node;
    length++;
}

void LinkedList::add_2(double item) {
    struct Node *node = new Node();
    node->data = item;
    node->next = head;

    head = node;
    length++;
}

void LinkedList::printLL() {
    struct Node *cur_node;
    cur_node = head;
    int i = 0;
    while (i < length) {
        cout << cur_node->data << " ";
        cur_node = cur_node->next;
        i++;
    }
}




int main() {
    LinkedList ll = LinkedList();
    ll.add_2(212);
    ll.add_2(2123);
    //ll.printLL();
    ll.add_2(2123);
    ll.printLL();
    return 0;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM