C 的 链表节点
等级 5289 次按键
声明自引用的 Node 结构体,创建存放 10、20、30 的三个节点并用 next 指针连起来,然后从第一个节点遍历到 NULL,打印 10 20 30。
C 代码
// 自引用结构体:包含指向自身类型的指针1struct Node { // 节点中存放的值2 int data; // 指向同类型下一个节点的指针3 struct Node *next;4};5 // 创建三个尚未相连的节点;每个 next 初始为 NULL6struct Node n1 = {10, NULL};7struct Node n2 = {20, NULL};8struct Node n3 = {30, NULL};// 连接 n1 → n2 → n3;n3.next 仍为 NULL,表示链表结束9n1.next = &n2;10n2.next = &n3;11 // 从第一个节点开始遍历12struct Node *current = &n1;// 沿 next 指针一直走到表示链表末尾的 NULL13while (current != NULL) { // 打印当前节点的 data:依次为 10、20、3014 printf("%d ", current->data); // 移动到下一个节点15 current = current->next;16}// 用换行结束输出行17printf("\n");运行结果
10 20 30
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 C 代码中需要输入的符号及其出现次数。
;× 12=× 8{× 5}× 5,× 4"× 4(× 3)× 3&× 3-× 2