【发布时间】:2015-12-29 03:37:57
【问题描述】:
所以我是 C 新手,正在尝试创建我的链表。但由于某种原因,我不断收到这个error: unknown type name List。这是我目前所拥有的:
struct Node{
int data;
struct Node *next;
};
struct List{
struct Node *head;
};
void add(List *list, int value){
if(list-> head == NULL){
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
newNode->data = value;
list->head = newNode;
}
else{
struct Node *tNode = list->head;
while(tNode->next != NULL){
tNode = tNode->next;
}
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
newNode->data = value;
tNode->next = newNode;
}
}
void printNodes(struct List *list){
struct Node *tNode = list->head;
while(tNode != NULL){
printf("Value of this node is:%d", tNode->data);
tNode = tNode->next;
}
}
int main()
{
struct List list = {0}; //Initialize to null
add(&list, 200);
add(&list, 349);
add(&list, 256);
add(&list, 989);
printNodes(&list);
return 0;
}
我来这里寻求帮助,因为我不知道如何在 C 中调试,并且来自 Java,对指针、内存分配和其他东西不太了解,我想知道我是否可能以某种方式搞砸了。我也得到了这个警告,也不知道这可能意味着什么。
警告:函数'add'的隐式声明[-Wimplicit-function-declaration]|
帮助表示赞赏。
【问题讨论】:
标签: c linked-list