SEGMENTATION FAULT IN LINKED LIST IMPLEMENTATION IN C++
I am writing a function in C++ to add a "data" of type 'int' to the end of
a linked list.
void insert_back()
{
int no;
node *temp;
cout<<"\nEnter the number"<<"\n";
cin>>no;
temp = head;
if(temp != NULL)
{
while(temp != NULL)
temp = temp->next;
}
temp->next = (node*)malloc(sizeof(node));
temp = temp->next;
temp->data = no;
temp->next = NULL;
}
However, at the line, temp->next = (node*)malloc(sizeof(node)), I get an
access violation error(segmentation fault). I do not find anything
fundamentally wrong. Can you please enlighten me on the issue?
No comments:
Post a Comment