简体   繁体   English

链表中的指针

[英]Pointers in linked lists

I'm studying lists currently (trying to recreate them) and I came across a weird problem. 我目前正在研究列表(试图重新创建它们),但遇到了一个奇怪的问题。 Here's my struct: 这是我的结构:

struct listNode{
    listNode(int n, listNode* ne = NULL){
        value = n;
        next = ne;
    }
    int value;
    listNode* next;
};
listNode* head = NULL;

Now I made a function to add an element to the bottom: 现在,我做了一个函数,将元素添加到底部:

void add(int n){
    if(head == NULL){
        head = new listNode(n);
        return;
    }
    listNode* n1 = head;
    while(n1 != NULL){ //Should be: while(n1->next != NULL){
        n1 = n1->next;
    }
    n1 = new listNode(n); //Should be: n1->next = new listNode(n);
}

But this isn't adding any element past the head. 但这并没有增加任何其他因素。 Now, I already figured out the solution (see comments above) my problem is that I do not understand why my first function didn't work. 现在,我已经找到了解决方案(请参阅上面的注释),我的问题是我不明白为什么我的第一个功能无法正常工作。
I'll explain what I understood with a scheme: 我将用一个方案解释我的理解:

The Beginning 起点
HEAD = NULL; HEAD = NULL;

I add 1 我加1
HEAD = [1, NULL]; HEAD = [1,NULL];

I add 2 我加2
The while loop arrives at the last element (where "next" is NULL) and creates in it the new element while循环到达最后一个元素(其中“ next”为NULL)并在其中创建新元素
HEAD = [1, new listNode(2)]; HEAD = [1,新的listNode(2)];
Result 结果
HEAD = [1, POINTER] [2, NULL]; HEAD = [1,POINTER] [2,NULL];

Now, why n1 after the while loop isn't what I wan't it to be? 现在,为什么while循环之后的n1不是我想要的?

You can think of it this way: no matter what you used to do within the while loop, the condition of the while loop was such that the loop would only terminate once n1 became null . 您可以这样想:无论您在while循环中曾经做过什么, while循环的条件都是这样,使得循环仅在n1变为null终止。 So, the value of n1 was guaranteed to be null after the loop. 因此,保证循环后n1的值为null

However, the value of n1 after the while loop was irrelevant, because you were not using it after the while loop. 但是,while循环之后的n1值是无关紧要的,因为在while循环之后您没有使用它。

On the other hand, your last instruction was n1 = new listNode(n); 另一方面,您的最后一条指令是n1 = new listNode(n); so you were creating a new listNode, and assigning it to n1 , which you were then forever forgetting by leaving the function. 因此,您正在创建一个新的listNode,并将其分配给n1 ,然后通过离开该函数将其永远遗忘。 (So, the new node was being leaked.) (因此,新节点被泄漏。)

Is is simple, 很简单,

while(n1 != NULL){
    n1 = n1->next;
}

// n1 here is null
// head here is [1, NULL]

n1 = new listNode(n);

// n1 here is something
// head here is [1, NULL]

so unless you set the previous element's next pointer of the head to your new element, it won't work 因此,除非您将头部的上一个元素的下一个指针设置为新元素,否则它将不起作用

The function does not work becuase n1 is a local variable of the function. 该函数无效,因为n1是该函数的局部变量。 Any changes of the variable do not influence on other nodes of the list. 变量的任何更改都不会影响列表的其他节点。

You should change the original nodes of the list. 您应该更改列表的原始节点。 The function can be written the following way 该函数可以通过以下方式编写

void add( int value )
{
    listNode **node = &head;

    while ( *node ) node = &( *node )->next;

    *node = new listNode( value );
}

In this case because variable node points to the actual fields of the list it indeed changes them. 在这种情况下,由于变量node指向列表的实际字段,因此确实会更改它们。

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

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