我正在尝试创建一个append_node方法来将节点添加到我创建的链接列表中。 我的节点结构定义如下:
typedef struct Node{
struct Node next = NULL;
int id;
} Node;
但是,使用以下方法进行编译时,会出现以下错误: 'Node'没有名为'id'的成员 'Node'没有名为'next'的成员
void append_node(Node *sent,int val){
Node *other_node = (struct Node *)malloc(1*sizeof(struct Node));
other_node->id = val;
Node n = *sent;
while (n.next != NULL){
n = n.next;
}
n.next = other_node;
}
为什么会出现此错误?
编辑:
我也有以下错误
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
在节点定义的第一行
答案 0 :(得分:2)
您不能在同一结构中再次定义Node。这将是无限递归。
你可以使用相同类型的指针。
typedef struct Node{
struct Node *next;
答案 1 :(得分:1)
您的代码中存在大量错误。
这是一个正确的版本
typedef struct NodeTag
{
struct NodeTag* next;
int id;
} Node;
void append_node(Node* sent,int val)
{
Node* other_node = (Node*)malloc(sizeof(Node));
other_node->id = val;
other_node->next = 0;
Node* n = sent;
while (n->next != 0)
{
n = n->next;
}
n->next = other_node;
}