【发布时间】:2014-04-24 16:37:44
【问题描述】:
我正在学习 C 中的链表。我不熟悉使用传递引用来操作链表。现在我知道我在这个程序中做了一些非常愚蠢的事情。该程序创建一个列表,然后基本上返回特定值的实例数(节点的数据)。我在 main 的每个语句之前收到一个错误,“预期的声明说明符”!。
怎么了?
#include<stdio.h>
#include<malloc.h>
struct list {
int number;
struct list *next;
};
typedef struct list node;
void create(node *);
int count(node **,int);
main()
int key,this_many;
node *head;
head = (node *)malloc(sizeof(node));
create(head);
printf("Which number?\n");
scanf("%d",&key);
this_many = count(&head,key);
printf("%d times\n",this_many);
return 0;
}
void create(node *list) {
printf("Enter a number -999 to stop\n");
scanf("%d",&list->number);
if(list->number == -999) {
list->next = NULL;
}
else {
list->next = (node *)malloc(sizeof(node));
create(list->next);
}
}
int count(node **addr_list,int key) {
int count = 0;
while(*addr_list != NULL) {
if((*addr_list)->number == key) {
count++;
}
*addr_list = (*addr_list)->next;
}
return(count);
}
【问题讨论】:
-
我觉得你的牙套不平衡?你能把代码格式化一下吗?
-
我没有看到
main()起始括号{ -
omg..感谢一个非常愚蠢的错误,它现在运行完美。
标签: c list linked-list