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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -