I'm new to learning data stucture in C. I wrote the code as follows when following a youtube video tutorial:
#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();
}
}
But it keeps complaining
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));
I'm pretty new to both C and data structure. Can anyone please tell me where the problem is? The code is mainly to insert nodes at the beginning of the list. Thanks in advance.