单链表求表长
1. 计数器从 0 开始
表长只统计数据结点,头结点不计入长度。
头next
a1next
current
int length = 0;
LNode *current = head->next;
2. 每访问一个数据结点就加 1
沿 next 走到 NULL 为止。循环次数就是数据结点个数。
while (current != NULL) {
length++;
current = current->next;
}
3. 返回 length
空表时 head->next == NULL,循环不会执行,结果自然为 0。
return length;