2014-04-24 270 views
1

我正在学习C中的链接列表。我是使用传递引用来处理链接列表的新手。现在我知道我在这个程序中做了一些非常愚蠢的事情。这个程序创建一个列表,然后基本上返回一个特定值的实例数(节点的数据)。在每次main!语句之前,我会收到一个像这样的错误,“Expected declaration specifier”。预期声明说明符

这是怎么回事?

#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); 
    } 
+0

我觉得你的括号不均衡?你能否将代码格式化得更好一些? – demongolem

+2

我没有看到'main()'的开始括号'{' – brokenfoot

+0

omg ..感谢一个非常愚蠢的错误,它确实现在完美运行。 – tofu

回答

0

问题:

  1. 您还没有指定的main返回类型。
  2. 您没有{来启动main的范围。

变化的线条开始main到:

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; 
}