ael*_*lor 2 c struct linked-list
我有一个代码如下:
struct point {
int a;
int b;
}
Run Code Online (Sandbox Code Playgroud)
可以进一步使用如下:
struct point p;
p.x = 10;
p.y = 5;
Run Code Online (Sandbox Code Playgroud)
现在我开始知道这也可以这样写:
typedef struct{
int x;
int y;
} point;
Run Code Online (Sandbox Code Playgroud)
并可以用作 point p
当我开始学习链表时,混乱就开始了,这就是我看到的代码.
typedef struct node {
int val;
struct node * next;
} node_t;
Run Code Online (Sandbox Code Playgroud)
我有一些问题:
typedef struct { ... } node什么来编写结构来使用写作typedef struct node {.....node_t,因为从我的理解,它已经定义的类型的代码的到底是真是混乱node,所以我们可以调用node x创建一个节点,那有什么的需要node_t或之后基本上可以写任何东西}是什么意思?这不应该工作吗?
typedef struct {
int val;
struct node * next;
} node;
Run Code Online (Sandbox Code Playgroud)
如果我们可以通过简单地使用
typedef struct { ... } node什么来编写结构来使用写作typedef struct node {.....的
node_t,因为从我的理解,它已经定义的类型的代码的到底是真是混乱node,所以我们可以调用node x创建一个节点,那有什么的需要node_t或之后基本上可以写任何东西}是什么意思?
在C中,结构可以同时包含标记和typedef名称.在结构声明中:
typedef struct node {
int val;
struct node * next;
} node_t;
Run Code Online (Sandbox Code Playgroud)
node是一个标签,node_t是typedef名称.之后,您可以使用标记或typedef名称声明结构变量.
struct node *new_node; // OK
node_t *head; // OK
Run Code Online (Sandbox Code Playgroud)
事实上,tag和typedef名称甚至可以相同,尽管这不是必需的:
typedef struct node {
int val;
struct node * next;
} node;
Run Code Online (Sandbox Code Playgroud)
这不应该工作吗?
typedef struct {
int val;
struct node * next;
} node;
Run Code Online (Sandbox Code Playgroud)
不,这不行.为什么?
原因在于,当结构具有指向与节点相同类型结构的成员时,我们需要使用结构标记.没有node标签,我们就无法声明其类型next.
建议阅读:由于Op正在寻求有关数据结构的良好资源.在这里你可以去: