I have following code to insert an element in LinkedList. I havebeen following tutorials and I am unable to spot error in this code.
I am using DEVC++ and it gives me a compile time error that says: [Error] 'Node' undeclared (first use in this function)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head; //global variable
int main(){
head = NULL; //empty list
int n, i, x;
printf("How many numbers would you like to enter");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("Enter the number you want to add to list:");
scanf("%d", &x);
Insert(x);
Print();
}
}
void Insert(int x){
Node* temp= (Node*)malloc(sizeof(struct Node)); //I get error here
temp->data = x;
//temp->next = NULL; //redundant
//if(head!= NULL){
temp->next = head; //temp.next will point to null when head is null nd otherwise what head was pointing to
//}
head = temp;
}
void Print(){
struct Node* temp1 = head; //we dont want tomodify head so store it in atemp. bariable and then traverse
while(temp1 != NULL){
printf(" %d", temp1->data);
temp1= temp1->next;
}
printf("\n");
}