Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
struct Node
{
    int data;
    struct Node *next;
}

void ReversePrint(Node *head)
{
    if(head->next!=NULL)
    {
      ReversePrint(head->next);
      cout<<head->data<<endl;
    }
    else
    {
       cout<<head->data<<endl;
       return;
    }
}


What I have tried:

i tried to print the elements of a linked list in reverse direction but i get a run time error during the execution
Posted
Updated 9-Sep-16 10:24am
v2

This is your homework: and writing the code is only a small part of the task - the big bit is testing it and making it work exactly as it needs to to satisfy the problem requirements. So, it's going to be up to you - so use the debugger.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Make sure the code you didn't post is building the list correctly.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900