⚠️ 头插三次,走出来的顺序

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

(本条路线统一用 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;
}
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论