简体   繁体   中英

What does the AND operator mean when applied to pointers in linked list?

I am trying to comprehend the C code written to detect and remove a loop in a linked list (taken from here ). While everything else makes sense to me, I am unable to understand what is going on inside the while statement. More specifically, how does the logical AND behave when applied to pointer structures?

while (slow_p && fast_p && fast_p->next) 

A hare and tortoise approach has been followed here wherein we use two pointers, one fast and one slow.

/* Link list node */
struct Node 
{ 
    int data; 
    struct Node* next; 
}; 

/* Function to remove loop. Used by detectAndRemoveLoop() */
void removeLoop(struct Node *, struct Node *); 

/* This function detects and removes loop in the list 
  If loop was there in the list then it returns 1, 
  otherwise returns 0 */

int detectAndRemoveLoop(struct Node *list) 
{ 
    struct Node  *slow_p = list, *fast_p = list;

while (slow_p && fast_p && fast_p->next) 
{ 
    slow_p = slow_p->next; 
    fast_p  = fast_p->next->next; 

    /* If slow_p and fast_p meet at some point then there 
       is a loop */
    if (slow_p == fast_p) 
    { 
        removeLoop(slow_p, list); 

        /* Return 1 to indicate that loop is found */
        return 1; 
    } 
} 

/* Return 0 to indeciate that ther is no loop*/
return 0; 

}

What you have is a condition, that ensures the loop breaks and exits if either of three pointers is NULL . A null pointer always evaluates to boolean false in C. See https://en.wikipedia.org/wiki/Null_pointer#Null_pointer .

With the current logic though, it is likely that none of the pointers would be NULL if a cycle exists in your list because we detect the loop and break out of it. The fast_p pointer could be NULL only when there is no loop in your list and you just finished traversing the whole list in attempt to finding one.

在此处输入图片说明

The sequence of execution happens as follows

  1. slow_p will be at node 2 and fast_p will be at node 3
  2. slow_p will be at node 3 and fast_p will be at node 5
  3. slow_p will be at node 4 and fast_p would have gone past the cyclic node and would be at node 3 again
  4. slow_p will be at node 5 and fast_p will be at node 5

At this point both these pointers are pointing to the node at the same address, so the address comparison would assert causing the function to break and return the address of slow_p which is now exactly pointing at the node causing this cyclic loop.

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