【发布时间】:2019-11-18 08:01:37
【问题描述】:
struct Node *addToEmpty(struct Node *last, int data)
{
// This function is only for empty list
if (last != NULL)
return last;
// Creating a node dynamically.
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
// Assigning the data.
temp -> data = data;
last = temp;
// Creating the link.
last -> next = last;
return last;
}
struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
return last;
}
我想知道为什么使用“*addToEmpty”而不是“addToEmpty”。
结构中的“*”是什么意思?
我知道这是基本问题。但我找不到答案。
如果你回答我的问题,我今天会很充实
附:这是 C++ 代码。
【问题讨论】:
-
返回类型为
Node*(指向节点的指针)。试着这样读:struct Node* addToEmpty(struct Node *last, int data) -
查看函数的返回值。你返回一个指向节点的指针,返回类型是
Node *<function_name>。对我来说,如果它写成Node* <function_name>,我会更喜欢它 -
这比 C++ 更多的是 C
-
stackoverflow.com/questions/6990726/… 试图解释为什么(一些)C 程序员这样声明指针。
-
相关:Use of '&' operator before a function name in C++。这里也是同样的问题,只是使用
*而不是&。它们都适用于返回类型,而不是函数名。