【发布时间】:2017-08-22 17:19:49
【问题描述】:
我正在尝试用 C 编写一个 LinkedList。这是我的两个结构
struct node{
int key;
int value;
struct node *next;
};
struct LinkedList {
struct node *head;
};
这是我创建新节点的方法。
void createNode(int key, int value) {
struct node *new_node;
new_node->key = key;
new_node->value = value;
new_node->next = lList->head;
lList->head = new_node;
}
我正在尝试使用下面的函数遍历 LinkedList。
void traverseNode(struct LinkedList *lList) {
struct node current = *lList->head;
while(current != NULL) {
printf("%i", current->key);
current = current->next;
}
}
但是,我一直收到错误提示
invalid operands to binary expression ('struct node'
and 'void *')
关于我的while 表达式。
另外,我收到一个错误
printf("%i", current->key);
current = current->next
错误是
成员引用类型'struct node'不是指针; 也许你打算使用 '.'
我很困惑,因为我认为在我的节点结构中,*next 被定义为一个指针,因此只能使用间接(->)语法来访问。
我是指针的初学者,因此感谢任何帮助。
【问题讨论】:
标签: c pointers struct linked-list