⚠️ 头插三次,走出来的顺序
(本条路线统一用 gcc -std=c11 -O0 编译)每次都插到最前面:
#include <stdio.h>
#include <stdlib.h>
struct Node { int v; struct Node *next; };
int main(void) {
/* 头插三次,链上的顺序正好是倒过来的 */
struct Node *head = NULL;
for (int i = 1; i <= 3; i++) {
struct Node *n = malloc(sizeof(struct Node));
n->v = i;
n->next = head;
head = n;
}
int first = head->v;
int last = 0;
for (struct Node *c = head; c != NULL; c = c->next) last = c->v;
struct Node *c = head;
while (c != NULL) { struct Node *nx = c->next; free(c); c = nx; }
printf("%d/%d\n", first, last);
return 0;
}
全部评论