我是新來的C. 學習數據stucture我寫的代碼如下YouTube視頻教程的時候如下:數據結構 - 在列表的開頭插入節點
#include "stdlib.h"
#include "stdio.h"
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void Insert(int x){
struct Node* temp = (Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Print(){
struct Node* temp = head;
printf("Now the list is: \n");
while(temp != NULL){
printf("%d\n", temp->data);
temp = temp->next;
}
}
int main(){
head = NULL;
printf("How many numbers?\n");
int n,x,i;
scanf("%d", &n);
for(i=0;i<n;i++){
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
}
但它不斷抱怨
aaa.c: In function ‘Insert’:
aaa.c:12:23: error: ‘Node’ undeclared (first use in this function)
struct Node* temp = (Node*)malloc(sizeof(struct Node));
^
aaa.c:12:23: note: each undeclared identifier is reported only once for each function it appears in
aaa.c:12:28: error: expected expression before ‘)’ token
struct Node* temp = (Node*)malloc(sizeof(struct Node));
我很新的C和數據結構。任何人都可以告訴我問題在哪裏?代碼主要是在列表的開頭插入節點。提前致謝。
[在C中,你不應該施放'malloc'的結果。](http://stackoverflow.com/questions/605845/do-i-cast - 結果的malloc) – 2014-10-16 10:17:38
最小的測試用例請;) – 2014-10-16 10:17:43
檢查此問題以瞭解發生了什麼:http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – midor 2014-10-16 10:35:09