【问题标题】:Why pointer used in node of a linked list is of type struct node and not data.Node?为什么链表节点中使用的指针是struct node而不是data.Node?
【发布时间】:2021-06-29 15:01:08
【问题描述】:

这是代表节点的结构声明

  struct Node
{ 
     datatype data;
     struct Node *next;
}

【问题讨论】:

  • datatype.Node 是什么意思?
  • 如果Node 包含一个Node,它包含一个Node,它包含一个Node,它包含一个Node,那么它的大小是多少? Node,其中包含一个Node,其中包含一个Node,...?

标签: c struct scope declaration


【解决方案1】:

在此声明中的 C 中(您忘记放置分号)

struct Node
{ 
     datatype data;
     struct Node *next;
};

声明了类型说明符struct Node。没有声明类型说明符Nodestruct NodeNode 不等价。

要引入名称 Node 作为类型说明符 struct Node 的别名,您可以这样写

typedef struct Node Node;

struct Node
{ 
     datatype data;
     Node *next;
};

在 C++ 中你可以写

struct Node
{ 
     datatype data;
     Node *next;
};

来自 C++ 17 标准(12 个类)

2 类名被插入到声明它的作用域中 在看到类名之后立即。 类名也是 插入到类本身的范围内;这被称为 注入类名

C 和 C++ 是两种不同的语言,它们各自的规则往往不一致。

例如,在 C 中,您可以编写像 sizeof( struct A { int x; } ) 这样的表达式,而在 C++ 中这样的表达式是无效的。

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2013-02-09
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多