Board index » delphi » Help Link List, Reverse Print List Node

Help Link List, Reverse Print List Node

Hi all programmer,
I wish to know how to Display a dynamic link lists from its last node to
the first node, using ** recursive function **.

Assume:
type
       Ptr = ^node
       Node = Record
           Element : Integer;
           Next : Ptr;
       End;
var
       Head : Ptr;

Thanks for any one helps me. urgent !!!

 

Re:Help Link List, Reverse Print List Node


Quote
Woo Shian Loong wrote:

> Hi all programmer,
> I wish to know how to Display a dynamic link lists from its last node to
> the first node, using ** recursive function **.

     The general method for dealing with recursion is to recognize how
the function is recursive, and the "boundary condition" that breaks the
recursion.  For example, if the goal were to produce a list of integers
from 1 to 10, the recursive call might be "If I've done this for the
number N, I need to do it for the number N+!", while the boundary
condition might be "If I've done this for 10, I'm done".

     Express your problem similarly.  When dealing with linked lists,
which have an inherently recursive structure, a "natural" method for
dealing with recursion is "Do whatever you are doing with the next
element in the list", while the natural terminating condition is "Don't
do anything with NIL".

     This should get you started.  Play around a little bit with these
ideas about recursion.  If you don't understand these concepts, then you
probably will not be able to solve this (and similar) problems!
Providing "the answer" won't really help.

Bob Schor
Pascal Enthusiast

Re:Help Link List, Reverse Print List Node


"Woo Shian Loong" <wslo...@pc.jaring.my> wrote:

Quote
>Hi all programmer,
>I wish to know how to Display a dynamic link lists from its last node to
>the first node, using ** recursive function **.

Think of you and a number of your friends standing in line.  Now
think of being handed a list of numbers by the person in front of
you.  After you tear the top number from the list, you pass the
remainder of the list to the person behind you.  After a moment or
two, the person behind you calls out a number, you respond by
calling out the number you took from the list, the person in front
calls out a number, ...

If you can't see the similarity between this and a recursive
routine, think of each individual as being the same routine at a
different instant of time.

    ...red

Re:Help Link List, Reverse Print List Node


Quote
Woo Shian Loong wrote:

> Hi all programmer,
> I wish to know how to Display a dynamic link lists from its last node to
> the first node, using ** recursive function **.

> Assume:
> type
>        Ptr = ^node
>        Node = Record
>            Element : Integer;
>            Next : Ptr;
>        End;
> var
>        Head : Ptr;

> Thanks for any one helps me. urgent !!!

I figured out non-recursive...
Const
  max=50{any integer you want}
Type
  Array [1..max] of ptr;

Begin
  for i:=1 to max do
    A[i]:= node;

  for i:= max downto 1 do
    writeln(i);
End.

Other Threads