c - 为什么链表的变量必须是指针

标签 c data-structures linked-list

我有下面的代码:

#include<stdio.h>

typedef struct node{
    int value;
    struct node *next;
}node;

int main(){
    node *a;
    return 0;
}

谁能向我解释为什么a必须是指针而不是变量类型节点?感谢您的帮助

最佳答案

链表的下一个节点由struct node *next;给出

根据定义,这必须是一个指针。不能将 struct node 变量包含在 struct node

的另一个变量中
typedef struct node{
    int value;
    struct node next;   //-- Not Allowed
}node;

上述声明将无法编译,因为结构节点的大小将是无限的。

链表的可以是非指针,但这不必要地使代码变得复杂,因为您需要分别为头和其他节点设置两个代码段。

关于c - 为什么链表的变量必须是指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367884/

相关文章:

将负二进制转换为十进制

algorithm - 有效地将数组转换为笛卡尔树

algorithm - 堆树 - 输出排序列表的复杂性

c - 链表字符指针scanf输入

PHP:如何在自定义模块/扩展中调用 echo

计数器的C程序

list - 在流中使用嵌套结构的 Getter

c - C 中带有链表和计数器的队列

java - Java中的链表有快速的concat方法吗?

c - 返回由malloc分配的数组