使用单链表的结构

时间:2014-12-09 12:30:25

标签: c singly-linked-list

我一直在想:

struct node
{
    int data;
    struct node *next;
}*head;

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

1 个答案:

答案 0 :(得分:5)

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

struct node
{
    int data;
    struct node *next;
};
struct node *head;

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

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

struct {int a, b;} *variable;