我是C语言学习数据结构的新手。在跟随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和数据结构都相当陌生。谁能告诉我问题出在哪里?该代码主要是在列表的开头插入节点。提前致谢。
下一行中的Node没有任何意义(struct Node *)
struct Node* temp = (Node*)malloc(sizeof(struct Node));//Wrong
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));//Right way to use
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句