The below code is supposed to add a node at the front of a linked list and print the current elements. But running this code gives me run time error and the program terminates. When asking for how many numbers and i typed a number and then it shows "main.cpp has stopped working". What might be wrong ?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
Node* next;
};
struct Node* head;
using namespace std;
void Insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
void Print()
{
Node* temp1=head;
while(temp1!=NULL)
{
printf("%d\n",temp1->data);
temp1=temp1->next;
}
printf("\n");
}
int main()
{
head=NULL;
printf("how many numbers?\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();
}
return 0;
}
scanf("%d", &n);Note the ampersand.