C语言链表节点中if(current->yes)条件的含义及判定逻辑咨询
current->yes条件 Hey there! Let's break down your two questions clearly using the code you shared:
1. if(current->yes)条件的含义是什么?
First, let's recap the struct definition you provided:
typedef struct node { char *question; struct node *no; struct node *yes; } node; node *current;
This node struct is the building block of a binary decision tree—each node holds a question, plus two pointers to child nodes: one for the "no" answer path, and one for the "yes" answer path.
The condition if(current->yes) is checking whether the yes pointer of the current node is not NULL. In C, any non-NULL pointer evaluates to true in a boolean context, while a NULL pointer evaluates to false.
So the full line:
if (current->yes) { current = current->yes; }
means: "If the current node has a valid 'yes' child node (i.e., there's a follow-up question/branch for a 'yes' answer), move the current pointer to that 'yes' child node."
2. 在if条件中,current->yes应判定什么内容?
At its core, current->yes is being checked to confirm two key things:
- Validity of the child node: We're verifying that the
yespointer points to an allocated, validnodeinstance (not NULL). This prevents us from trying to access memory that doesn't exist (which would cause a crash or undefined behavior). - Existence of a follow-up branch: In a decision tree structure, leaf nodes (nodes that don't have further questions) typically have their
yesandnopointers set to NULL. So this check tells us: "Is there a next step in the decision path when the user answers 'yes'?"
Put simply, you're checking if there's a valid "yes" branch to traverse before moving the current pointer to it.
内容的提问来源于stack exchange,提问作者Mody El Sayed

