struct Node{
int data;
struct Node* next;
};
struct Node* head;
void Insert(int x){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=NULL;
head=temp;
}
void Print(){
struct Node* temp=head;
printf("List is: \n");
do{
printf("%d", temp->data);
temp=temp->next;
}while(temp!=NULL);
}
int main(){
head = NULL;
printf("How many numbers do you want to put into a list? \n");
int n, i, x;
scanf("%d", &n);
for (i=0; i<n; i++){
printf("Enter the number: \n");
scanf("%d", &x);
Insert(x);
Print();
}
}
this code only prints the last element. I've checked it multiple times, and i can't seem to find the mistake. I would really appreciate If anyone could tell me where did it go wrong.
thanks in advance