简体   繁体   中英

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

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.

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. In this case, the first node.

Let's say we have a linked list with 4 nodes. Also p is a pointer to a pointer so it should always point to a pointer and not a node. 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 )

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. If it's *p do the same thing but one more level, so in this case the first node?

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; 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.

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. 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

For this case we are gonna follow the 2.1 approach. Now that the second node is 2 and it's even. 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.

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). 3

Are my logic and drawing correct? because that's what I'm trying to understand

Note - head is a global variable as coded. It could have been passed as a parameter, and the updated value of head returned.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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