2011-04-17 50 views
0
#include <stdio.h> 
#include <stdlib.h> 

typedef struct student { 
    int rollNo; 
    char studentName[25]; 
    struct student *next; 
}node; 

node *createList(); 
void printList(node *); 


int main() 
{ 
    node *head; 
    head = createList(); 
    void printList(node *head); 



    return 0; 
} 

node *createList() 
{ 
    int idx,n; 
    node *p,*head; 
    printf("How many nodes do you want initially?\n"); 
    scanf("%d",&n); 

    for(idx=0;idx<n;++idx) 
    { 
     if(idx == 0) 
     { 
      head = (node*)malloc(sizeof(node)); 
      p = head; 
     } 
     else 
     { 
      p->next = (node*)malloc(sizeof(node)); 
      p = p->next; 
     } 
     printf("Enter the data to be stuffed inside the list <Roll No,Name>\n"); 
     scanf("%d %s",&p->rollNo,p->studentName); 

    } 
    p->next = NULL; 
    p = head; 
    /*while(p) 
    { 
      printf("%d %s-->\n",p->rollNo,p->studentName); 
      p=p->next; 
    }*/ 

    return(head); 

} 

void printList(node *head) 
{ 
    node *p; 
    p = head; 
    while(p) 
    { 
     printf("%d %s-->\n",p->rollNo,p->studentName); 
     p=p->next; 
    } 
} 

这里可能有什么错误?我知道我做了一些愚蠢的事情,只是无法弄清楚它是什么。 我得到这些错误失踪';'之前输入'

error C2143: syntax error : missing ';' before 'type' 
error C2143: syntax error : missing '{' before '*' 
    error C2371: 'createList' : redefinition; different basic types 
+3

错误消息涉及什么行? – 2011-04-17 13:37:45

+0

这个编译对我来说很好。 – 2011-04-17 13:39:35

+0

错误C2143:语法错误:缺少';'在'type'之前>>> >>> printList(node * head); 错误C2143:语法错误:在'*&错误C2371:'createList'之前缺少'{':重新定义;不同的基本类型>>> node * createList() – Kelly 2011-04-17 13:40:55

回答

7
int main() 
{ 
    node *head; 
    head = createList(); 
    void printList(node *head); // This isn't how you call a function 
    return 0; 
} 

更改为:

int main() 
{ 
    node *head; 
    head = createList(); 
    printList(head); // This is. 
    return 0; 
} 
+0

这是一个程序员的错误,但它不应该导致编译器错误。 – 2011-04-17 13:40:53

+0

'void printList(node * head);'这不应该被当作声明吗?为什么它会导致任何问题呢? – Sadique 2011-04-17 13:42:55

+0

@Oli Charlesworth:用VS2008编译为C出现这个错误 - 编译为C++没有。 – Erik 2011-04-17 13:43:57

2

这条线main()是你的问题:

void printList(node *head); 

它应该是:

printList(head); 

你想在那里调用函数,而不是试图声明它。