C++ printing linked list , segmentation fault -
void print(node *head) { if (head = null) { cout << "null"; } else { cout << head->data << endl; head = head->next; while (head->next != null) { cout << head->data << endl; head = head->next; } cout << "null"; } }
i going assume line
if ( head = null ) is error in transcribing code, , working code uses
if ( head == null ) the real error see using
cout << head->data << endl; head = head->next; while (head->next != null) // line not { cout << head->data << endl; head = head->next; } cout << "null"; that line problem. @ point, head equal null , try access null pointer.
change block of code to:
while (head != null) { cout << head->data << endl; head = head->next; } cout << "null"; in fact, entire function can be:
void print(node* head) { while (head != null) { cout << head->data << endl; head = head->next; } cout << "null"; }
Comments
Post a Comment