简体   繁体   English

理解C++中指向指针和链表的指针背后的逻辑(绘制节点)

[英]Understanding the logic behind Pointers to Pointers and linked lists in C++ (drawing nodes)

I'm still trying to understand pointers, drawing nodes and everything but I can't seem to understand some things.我仍在尝试理解指针、绘制节点和所有内容,但我似乎无法理解某些事情。

For example here is a function that should delete nodes with even values from a list例如这里有一个 function 应该从列表中删除具有偶数值的节点

void delete_even()
{
    node **p= &head;

    while (*p)
    {
        if ((*p)->data % 2 == 0)
        {
           node *nextptr=*p;

           *p=(*p)->next;
           delete nextptr;
        }
        else
        {
            p= &(*p)->next;
        }
    }
}

So as I understand it.据我了解。 p is pointing to the pointer named head and head is pointing to the first node. p指向名为head的指针, head指向第一个节点。

If I write just p , I'm talking about the thing that p is pointing at, in this case the pointer head head If I write *p , I'm talking about the thing that p is pointing at and the one more data data dereferencing.如果我只写p ,我说的是p指向的东西,在这种情况下是指针 head head如果我写*p ,我说的是p指向的东西和另一个数据数据取消引用。 In this case, the first node.在这种情况下,第一个节点。

Let's say we have a linked list with 4 nodes.假设我们有一个包含 4 个节点的链表。 Also p is a pointer to a pointer so it should always point to a pointer and not a node.另外p是指向指针的指针,因此它应该始终指向指针而不是节点。 As I understand it.据我了解。 1. Now, while (*p) (In English that means: as long as the thing that p pointing at and one more data dereferencing ) 1.现在, while (*p) (英文意思是:只要p指向的东西和另一个数据取消引用)

I look at * as levels.我将*视为级别。 and * means return the value stored in the address kept in the pointer variable If it's just p the pointer look at the whatever the pointer points at.*表示返回存储在指针变量中的地址中的值如果它只是p指针,请查看指针指向的任何内容。 If it's *p do the same thing but one more level, so in this case the first node?如果它是*p做同样的事情但多了一个级别,那么在这种情况下是第一个节点?

Looking at the list we see that the first node is 1. It's not even so we take this part of the if statement: p= &(*p)->next;查看列表,我们看到第一个节点是 1。它甚至不是,所以我们采用 if 语句的这一部分: p= &(*p)->next; In English( Make the thing p points at = to the first node through dereferencing and and then getting the address of the next member of this value. In English( 通过解引用使p指向=的第一个节点,然后得到这个值的下一个成员的地址。

A)I'm not sure if p = ~to something~ makes the thing p is pointing at change it's value 2. That would make both pointers point at the second node. A)我不确定p = ~to something~ 是否使p指向的东西改变它的值2。这将使两个指针都指向第二个节点。 and the head pointer moved, I don't think it's right... B) or p = ~to something~ makes the pointer p point to something else 2.1并且头指针移动了,我认为这是不对的... B) or p = ~to something~ makes the pointer p point to something else 2.1

For this case we are gonna follow the 2.1 approach.对于这种情况,我们将遵循 2.1 方法。 Now that the second node is 2 and it's even.现在第二个节点是 2,它是偶数。 Make a new pointer named nextptr and make it point to the same thing *p is pointing at *p is currently pointing at the next pointer and the next is pointing to the second node.创建一个名为nextptr的新指针,并使其指向同一事物*p指向*p当前指向next指针,而next指向第二个节点。

Then we make the thing *p is pointing at currently which is the next data member that contains the address of the second node of the first node and make = to the second node's next (which is the address of the 3rd node).然后我们让*p当前指向的是next数据成员,它包含第一个节点的第二个节点的地址,并使 = 指向第二个节点的下一个(这是第三个节点的地址)。 3 3个

Are my logic and drawing correct?我的逻辑和绘图正确吗? because that's what I'm trying to understand因为这就是我想要了解的

Note - head is a global variable as coded.注意 - head 是编码的全局变量。 It could have been passed as a parameter, and the updated value of head returned.它可以作为参数传递,并返回 head 的更新值。

Initially:原来:

head = pointer to the first node
node**p = &head : p = ptr to head

If the first node value even, then:如果第一个节点值为偶数,则:

node *nextptr=*p; : nextptr = ptr to first node 
(*p) = (*p)->next : head = ptr to second node
delete nextptr    : delete first node

else if first node value not even否则如果第一个节点值不相等

 p = &(*p)->next; : p = ptr to (first node.next)
                    so that *p updates first node.next if second node even
                    and head remains pointer to first node

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

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