Ive gone through some of the previous posts on Stack Overflow before posting this. Everytime I try to run this on ideone.com (which uses the SPOJ engine) I get a SIGSEGV runtime error and I am stumped. Can anyone please help me out? Ive commented the line where I think I might have gone wrong..
//MY CODE TO INSERT INTO A LINKED LIST
#include<iostream>
#include<malloc.h>
using namespace std;
struct Node
{
int data;
struct Node *link;
};
struct Node *node =NULL; //suspected error..not sure
void insert(int item)
{
if(node==NULL)
{
node->data=item;
node->link=NULL;
}
else
{
struct Node *temp;
temp=node->link;
node->data=item;
node->link=temp;
}
}
void display()
{
if(node==NULL)
cout<<"Linked list is empty!";
while(node!=NULL)
{
cout<<node->data<<" ";
node=node->link;
}
}
int main()
{
int n;
cin>>n;
display();
insert(n);
display();
return 0;
}