使用单链表的结构

Rim*_*nas 0 c singly-linked-list

我一直想知道:

struct node
{
    int data;
    struct node *next;
}*head;
Run Code Online (Sandbox Code Playgroud)

我相信*head变量是链表的第一个指针,但为什么它写在结构括号之外?为什么我需要在整个结构之外写它?任何人都可以回答,因为我在整个链表中丢失了东西.如果它已经在整个"节点"结构中,为什么我们需要用"struct node"声明*next指针?

Pau*_*l92 5

head只是一个类型的指针struct node*.等效声明是:

struct node
{
    int data;
    struct node *next;
};
struct node *head;
Run Code Online (Sandbox Code Playgroud)

请注意,即使您没有声明所定义的结构类型的任何变量,也必须在结束大括号后添加分号.

这种语法的起源是struct是一种数据类型,声明变量的方式是指定它们的数据类型和名称.的确,你可以这样做:

struct {int a, b;} *variable;
Run Code Online (Sandbox Code Playgroud)