问题描述
这不是我的代码.我从这个网站上取下了这个代码:
http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html
我正在用作有关如何构建链表的参考资料.我对正在发生的事情感到有些困惑.有人可以向我解释发生了什么.我会用 1-5 标记让我困惑的地方.
#include#includestruct list_el {内部值;struct list_el * 下一个;};typedef struct list_el 项;无效主(){项目 * curr, * head;国际我;头 = NULL;//1for(i=1;i val = i;当前->下一个=头;//2头 = curr;//3}curr = 头;//4while(curr) {//5printf("%d
", 当前-> val);curr = curr->next ;}
head = NULL → 为什么 head 被设置为 NULL?我知道你应该这样做(我出于习惯这样做)但我真的不知道为什么.
curr->next = head → 我也从来没有真正理解过这一点.也许我对头"的定义有误,但在常规链表中,它是列表中的起始节点还是最后一个节点?我一直认为它是起始节点,但在这一行中,它看起来像是最后一个节点.
head = curr → 为什么我们将它设置为等于 curr?
curr = head → 然后在循环完成后设置 curr = head.
while(curr) → 只是为了确保,这是遍历列表,它等价于 while(curr != NULL) 对吗?
初始化指针.通常建议在 (1) 声明时或 (2) 声明后立即将指针初始化为 NULL.如果程序员错误地取消引用未初始化的指针,则会返回垃圾值.通常,如果您的静态分析器和编译器不显示未初始化指针的警告或错误消息,则调试起来非常困难.
有关详细信息,请参阅 Steve McConnell 的 Code Complete: A Practical Handbook of Software Construction 或 Defensive 上的维基百科页面编程.
#2:curr->next = head
建立链表.curr
节点正在链接"到序列中先前创建的节点.
#3:head = curr
更新头指针.head
指针正在更新以指向最近malloc
ed 的节点.
下面的插图形象化了第 2 步和第 3 步:
#4:curr = head
重新初始化指针.此步骤类似于步骤#2:curr->next = head
.通过将curr
节点设置为head
,curr
为while
循环中的链表遍历做好了准备".类似地,这就像在循环开始时将迭代变量初始化为 0(即 i = 0
).要可视化此步骤,请参阅以下显示此语句之前/之后的插图:
#5: while(curr)
遍历列表.鉴于 curr
指向第一个节点(来自第 4 步),这个 while
循环遍历列表直到 curr->next
返回空值.以不那么抽象的形式,我们可以将此语句重写为 while(curr != NULL)
.
This is not my code. I took this code off this website:
http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html
I am using for reference material on how to build a linked list. I'm a little confused on what is going on. Can someone please explain to me what is going on. I'll mark what is confusing me with 1-5.
#include<stdlib.h>
#include<stdio.h>
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
void main() {
item * curr, * head;
int i;
head = NULL; //1
for(i=1;i<=10;i++) {
curr = (item *)malloc(sizeof(item));
curr->val = i;
curr->next = head; //2
head = curr; //3
}
curr = head; // 4
while(curr) { //5
printf("%d
", curr->val);
curr = curr->next ;
}
head = NULL → why is head being set to NULL? I know that you are supposed to (I do it out of habit) but I don't really know why.
curr->next = head → I never really understood this as well. Maybe I have my definition of "head" wrong but in a regular linked list, is it the starting node or the last node on the list? I've always assumed it was the starting node but in this line it looks like it's the last node.
head = curr → Why are we setting it equal to curr?
curr = head → and then setting curr = head after the loop is done.
while(curr) → Just to make sure, this is traversing through the list and it is equivalent to while(curr != NULL) right?
Initializing the pointer. It's generally recommended to initialize the pointer to NULL either (1) at declaration or (2) immediately after declaration. If programmers mistakenly dereference uninitialized pointers, garbage values are returned. Often times, this is extremely hard to debug if your static analyzer and compiler don't display warning or error messages for uninitialized pointers.
For more information, please refer to Steve McConnell's Code Complete: A Practical Handbook of Software Construction or Wikipedia page on Defensive Programming.
Building the linked list. The curr
node is being "linked" to previously created node in the sequence.
Updating the head pointer. The head
pointer is being updated to point to the most recently malloc
ed node.
Below illustrations visualize Steps #2 and #3:
Re-initializing the pointer. This step is similar to step #2:curr->next = head
. By setting curr
node to head
, curr
gets "ready" for linked-list traversal in the while
loop. Analogically speaking, it's like initializing the iterating variable to 0 in the beginning of the loop (i.e. i = 0
). To visualize this step, please refer to the below illustrations showing before/after this statement is executed:
Traversing the list.Given that curr
is pointing to the first node (from step #4), this while
loop traverses the list until curr->next
returns NULL. In a less abstract form, we can rewrite this statement as while(curr != NULL)
.
这篇关于代码说明(链表C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!