为什么在链表中使用 struct 关键字和结构名称来创建节点指针
Why to use the struct keyword along with structure name in linked list to create pointer of node
struct linklist
{
int data;
struct linklist *next;
}
为什么在*next
前面使用struct linklist
?可以简单地通过 *node
?
创建一个指针
Why we are using struct linklist in front of '*next'
您不需要在此处使用详细的类型说明符。所以我想你使用它是因为你可以。
或者您可能打算跨语言边界使用该结构,在这种情况下,您使用它是因为有必要在 C 中使用它。
可以有同名的函数和结构。要将它们分开,您必须使用关键字 struct
.
E. g.
struct l {
int b;
};
void l(struct l &a) {
a.b = 5;
}
int main () {
struct l a;
l(a);
return 0;
}
在这种情况下,您不能省略关键字 struct
。但这是一种特殊情况,您可能会在较旧的源代码中找到它。
同样在 c 中你需要使用关键字。所以
struct l {};
int main() {
struct l a;
}
是有效的 c 和 c++ 代码但是
struct l {};
int main() {
l a;
}
仅是有效的 c++ 代码。
通常在 C++ 中不需要该关键字。
struct linklist
{
int data;
struct linklist *next;
}
为什么在*next
前面使用struct linklist
?可以简单地通过 *node
?
Why we are using struct linklist in front of '*next'
您不需要在此处使用详细的类型说明符。所以我想你使用它是因为你可以。
或者您可能打算跨语言边界使用该结构,在这种情况下,您使用它是因为有必要在 C 中使用它。
可以有同名的函数和结构。要将它们分开,您必须使用关键字 struct
.
E. g.
struct l {
int b;
};
void l(struct l &a) {
a.b = 5;
}
int main () {
struct l a;
l(a);
return 0;
}
在这种情况下,您不能省略关键字 struct
。但这是一种特殊情况,您可能会在较旧的源代码中找到它。
同样在 c 中你需要使用关键字。所以
struct l {};
int main() {
struct l a;
}
是有效的 c 和 c++ 代码但是
struct l {};
int main() {
l a;
}
仅是有效的 c++ 代码。
通常在 C++ 中不需要该关键字。