Jump to content

Recursive function t reverse a given list of numbers

- - - - -

  • Please log in to reply
5 replies to this topic

#1
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I need to write a recursive function that creates the given list and its exact opposite.
The list is : 1 2 3 null

and it's exact opposite : 3 2 1 null

Here's what I have tried but I am stucked in the middle.Someone please help me.


#include<stdio.h>

#include<stdlib.h>


typedef struct list{

int data;

struct list*next;

} LIST;


void add_list(int x,LIST *y)

{

LIST *c = (LIST *)malloc(sizeof(LIST));

c->data = x;

c->next = y->next;

y->next=c;

}


LIST* reverse_recurse(LIST* p,LIST* head)

{

        if(p->next==NULL)

        {

                head->next=p;

                return p;

        }

        else

        {

                reverse_recurse(p->next,head)->next=p;

        }

        return p;

}

int main(void)

{

        LIST* head;

        head=(LIST *)malloc(sizeof(LIST));

        int i=1;

        for(i=3;i>=1;i--)

                

*/have no idea how to do the rest.../




#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Try explaining your idea for the algorithm in words, first.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Do you understand how recursion works? This is the most important part of this exercise, and it's really not that hard to grasp.

About linked lists: when we started doing that topic in school, we first went the long way; we manually created each node, added it to list, moved list pointer. If you try this way, you'll see which part of code repeats itself => you have a function that adds to list. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Following is a code call thread where recursive linked list solution is discussed and it's problems are resolved. It does contain working code ultimately.
http://forum.codecal...-recursion.html

Here is another tutorial on link list reversal. Apart from reverse function, this contain list creation, node addition etc. functions that you seem to be having problems with above. It also discusses multiple solutions, recursion is one of which but it provides code for iterative one. However, if you are new to recursion it might be a good idea to understand iterative solution first and then modify it to work recursively i.e. replace the loop with a recursive call.
http://forum.codecal...ve-inplace.html

Hope that helps

Edited by fayyazlodhi, 21 January 2012 - 11:43 AM.
Adding description

Today is the first day of the rest of my life

#5
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

aruwin said:

I need to write a recursive function that creates the given list and its exact opposite.
The list is : 1 2 3 null

and it's exact opposite : 3 2 1 null


What do you mean

Quote

...and its exact opposite.
Seems like you are just traversing in an opposite way. Is that right? It's like doing
4 5 6 7 8
will give
8 7 6 5 4
. Is that what you're trying to achieve?

#6
CurlyBonesHopkins

CurlyBonesHopkins

    Newbie

  • Members
  • Pip
  • 7 posts
Try to use names for your variables instead of x,y,z and so on

Also try to comment out your code so that its easier for other programmers to debug your code

I don't see why you would want to have head -> next = p; in your base case, i didn't run your recursive code but just by looking at it I'm sure you don't need that line of code




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users