Click here to Skip to main content
14,580,625 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm practicing with linked lists using C. I'm a beginner programmer just started learning C two weeks ago.

I understand the concept of linked list very well and I was trying to reverse a linked list using recursion.

I know how to reverse it using a while loop, however with the recursion I'm stuck on how to point the head pointer to the last number address instead of the initial first number address after I reverse the list.

Below is my reverse function. The head is defined in main and I'm calling it by reference into my function. I know I can solve the issue by calling it by value and just writing in main() head=reverse(node* head); or by defining head in the dynamic memory and just accessing it straight from the function. I don't want to do that. I want to call it by reference from main and the function to return void.

Here is my reverse function:
void reverse(struct node** head)
{   
    struct node* p=*head;


    if(p->link==NULL)
    {  *head=p;
        return;
    }

    reverse(&(p->link));
    p->link->link=p;
    p->link=NULL;


}

Say The list is: 5 6 7 2 3 4 The output I'm getting after reverse is: 5
Thank you!

What I have tried:

I know the reason is because the head is still pointing at the first number address. I cant figure it out how to point it to the last number address. It should have done that in the if statement when it breaks the recursion function. But its not working.
Posted
Updated 21-Sep-16 23:27pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

First off ask yourself why you want the function to not return anything and why you want to modify the function argument. If you think you've got a good reason then have a look at functional programming and why functions in that never modify their arguments.

By giving yourself the requirements you have you're essentially trying to solve a problem in a deliberately obscure and impractical way. Just because C is great at manipulating addresses doesn't mean it's a good idea to mess around with raw memory when there's a cleaner way of doing it.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100