【发布时间】:2012-12-27 16:30:05
【问题描述】:
我正在研究以下算法来展平“编程面试暴露”一书中的树状链表系统:
void flattenList( Node *head, Node **tail)
Node *curNode = head;
while( curNode ){
/* The current node has a child */
if( curNode->child ){
append( curNode->child, tail );
}
curNode = curNode->next;
}
}
/* Appends the child list to the end of the tail and updates
* the tail.
*/
void append( Node *child, Node **tail ){
Node *curNode;
/* Append the child child list to the end */
(*tail)->next = child;
child->prev = *tail;
/*Find the new tail, which is the end of the child child
*list.
*/
for( curNode = child; curNode->next;
curNode = curNode->next )
; /* Body intentionally empty */
/* Update the tail pointer now that curNode is the new
* tail.
*/
*tail = curNode;
}
我知道我们将 **tail 而不是 *tail 传递给 append 函数以确保对 *tail 的更改持续存在,但我不明白为什么我们不这样做对于孩子(意思是我们为什么不通过**child 而不是*child)?
【问题讨论】:
标签: c algorithm linked-list