2011-04-17 55 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); 

你想在那裏調用函數,而不是試圖聲明它。