以下是的结构
struct list *node
{
int data;
struct list *next;
}
node=node->next在遍历链表时到底做了什么?下一个元素不是指向node变量中指针的指针吗?
想象一下用while
循环递增一个变量。
int i = 0;
while (i < 6) {
// this loop will run forever without the line below
i++; // adds one to i so the loop will not run infinitely
}
链接列表的概念相同:
while (node != NULL) {
//need to make sure this loop doesn't run forever
node = node->next // kind of like incrementing i
}
现在while循环将一直运行到列表的末尾。
node=node->next使节点指向链表中的下一个指针。由于存储在next中的值是指向列表中下一个元素的指针。因此,将node设为node->next,然后再次调用该函数或在for循环中迭代,可以继续前进。希望对有所帮助