I want to implement an insertion function on cs50 study but it doesn't work right. the program doesn't output the numbers I inserted. all i am sure about is that the problems is with my function .i need help that's my code

1 Answer 1


Your first problem is here:

for(node* ptr = head ; ptr != NULL ; ptr = ptr->next )

The for loop never executes because head is null when it hits the first time and the code to assign the first element in the list is inside the loop. This means that the for loop will never execute.

Once this is fixed, there are other issues. Try testing with 6 elements, and with duplicate numbers. This will be a good debugging exercise for you.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .