在这个节点定义中,为什么我们在结构定义之后使用"Node"?
In this node definition, why are we using the "Node" after the structure definition?
typedef struct node
{
int data;
struct node *next;
}Node; // ???
我不明白为什么我们在定义后使用节点。执行什么功能?它是否阻止结构无限指向自身?这个问题可能听起来很愚蠢,因为我仍在学习。谢谢。
结构定义之后有一个Node
,因为之前有一个typedef
。
结构定义本身是这样的:
struct node
{
int data;
struct node *next;
};
由于这是C++,因此足以通过名称来引用结构类型 node
.然而,似乎编写它的人最初要么来自 C 背景,要么旨在提供与 C 兼容的标头。在 C 中,你必须将其称为 struct node
,有些人觉得很冗长。因此,相关人员立即提供了一个类型别名。您提供的代码等效于:
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
它允许我们用名称来引用相同的类型 Node
,在 C 或 C++ 中。
在这里,您已经定义了一个结构,并同时声明了一个结构变量。 typedef
关键字用于创建类型Node
,其类型为 struct node
。
因此,您可以轻松地使用Node n1;
而不是 struct node n1
.
相关文章:
- 在类定义之后定义一个私有方法
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- 实例化具有不完整类型的类模板格式不正确(如果该类型是在之后定义的)
- 在 ss.clear() 之后使用 ss.str( " ") 用于新定义的字符串流
- 在这个节点定义中,为什么我们在结构定义之后使用"Node"?
- 预处理之后和使用CMake编译之前的自定义构建步骤
- C 以任何方式将类定义放置在main()函数之后
- 为什么编译器在我定义_CRT_SECURE_NO_WARNINGS之后仍然警告我不安全的strtok
- 不能在嵌套类中使用父类 - 即使之后定义了嵌套类
- 标识符在包含头之后使用未定义的类
- 在包含windows.h:语法错误之后无法定义枚举
- 是在类定义之后允许的类声明
- 在定义成员类之后重新声明它是否合法?
- 未定义的引用错误,即使在链接c文件之后
- 在结构类型之后定义结构变量
- 为什么main()函数之后的类定义不能工作
- 我应该在知道我将使用多少个元素之前还是之后定义向量中的元素数?
- 如何拥有在当前类之后定义的类的变量
- 在对象定义之后调用构造函数