0

I am trying to implement an insert function of the linked but as soon as I add the third element the program just crashes and execution is stopped, even though the same code worked on hackerrank's compiler.

Here is my code.

#include<bits/stdc++.h>
using namespace std;
class Node{
 public:
 int data;
 Node * next;
 Node(int data){
 this -> data = data;
 this -> next = nullptr;
 }
};
Node * insert_tail(Node * head, int data){
 Node * node = new Node(data);
 if(head == nullptr) return node;
 Node * current = head;
 while(head -> next != nullptr) current = current -> next;
 current -> next = node;
 return head;
}
void print_linkedlist(Node * head){
 while(head -> next != nullptr){
 cout << head -> data << " -> ";
 head = head -> next;
 }
 cout << head -> data << " -> nullptr";
}
int main(){
 Node * head = nullptr;
 head = insert_tail(head, 1);
 head = insert_tail(head, 5);
 head = insert_tail(head, 3);
 head = insert_tail(head, 5);
 head = insert_tail(head, 8);
 head = insert_tail(head, 17);
 print_linkedlist(head);
 return 0;
}
Vlad from Moscow
313k27 gold badges203 silver badges357 bronze badges
asked Oct 28, 2020 at 19:27
5
  • Which line does the execution crash on? Commented Oct 28, 2020 at 19:28
  • You should learn to use a debugger and step through the code to see what's happening. Commented Oct 28, 2020 at 19:36
  • Sorry about that I added the definition, but as @MikeCAT mentioned it was a logical error. Commented Oct 28, 2020 at 19:36
  • I am new to C++ can you send me some tutorial on how to use debugger. Commented Oct 28, 2020 at 19:37
  • Tactical note: Loops like while(head -> next != nullptr) are risky and require additional tests to ensure that head is valid before the first head -> next. I usually find it better to write while(head != nullptr) and build the initial test into the loop. Commented Oct 28, 2020 at 19:58

2 Answers 2

1

The line

 while(head -> next != nullptr) current = current -> next;

in the function insert_tail is wrong. It will run endlessly when head->next is not nullptr.

It should be

 while(current -> next != nullptr) current = current -> next;
answered Oct 28, 2020 at 19:29

Comments

1

Here is a typo

while(head -> next != nullptr) current = current -> next;
 ^^^^^^^^^^^^

Write

while(current -> next != nullptr) current = current -> next;
 ^^^^^^^^^^^^

An alternative definition of the function can look the following way,

void insert_tail( Node * &head, int data )
{
 Node **current = &head;
 while ( *current ) current = &( *current )->next;
 *current = new Node( data );
}

And the function can be called simply like

insert_tail(head, 1);

Also the function print_linkedlist can be written like

std::ostream & print_linkedlist( const Node * head, std::ostream &os = std::cout )
{
 for ( ; head; head = head->next )
 {
 os << head -> data << " -> ";
 }
 return os << "nullptr";
}

and can be called like

print_linkedlist(head) << '\n';
answered Oct 28, 2020 at 19:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.