【问题标题】:Quickly make a static linked list快速制作静态链表
【发布时间】:2015-03-22 08:12:40
【问题描述】:

我想快速做一个静态链表,代码尽量少,可读性好,不杂乱。我如何优雅地做到这一点?

类似

1 -> 2 -> 3 -> 4 -> NULL

【问题讨论】:

    标签: c linked-list


    【解决方案1】:
    struct node {int x; struct node *next;};
    #define cons(x,next) (struct node[]){{x,next}}
    struct node *head = cons(1, cons(2, cons(3, cons(4, NULL))));
    

    【讨论】:

    • @ArjunSreedharan.: 请解释第二行。
    • @amalsom 该数组只有 1 个元素,其中包含整数 x 和指针 next。这里next 将是下一个node[]NULL 的地址。
    • @ArjunSreedharan.: 像(struct node[]){{x,next}} 这样的语句怎么可能在语法上是正确的。这个语句是初始化吗?你能举出与 C 语言中通常使用的类似的例子吗?
    • 是的,绝对有一对{}太多了。
    • @ArjunSreedharan.: 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多