Mail Archives: djgpp/1999/04/24/10:35:27
A linked list is an iterative structure, not a recursive one.
A tree structure is recursive.
As was mentioned earlier, this makes an interesting stunt (stunt?!),
but is a bit like peeling oranges with a potato peeler. There are much
better and more efficient ways, so why make life difficult.
You wouldn't rewrite the code 'for (n=0 ; n<100; n++)' as a recursive
routine... or would you?
Cheers,
Jason
On Fri, 23 Apr 1999 23:57:49 GMT, seth AT kansmen DOT com (Seth Jones) wrote:
>In article <3720D21C DOT 9617323B AT atlantis DOT stortek DOT com>,
>matthto AT atlantis DOT stortek DOT com says...
>> Ishpeck wrote:
>> >
>> > Is it fezible to have a linked list initiate itself recursively? I
>> > think the best way to describe what I'm trying to say is put some
>> > code up...
>> >
>> > class fooclass {
>> > int number;
>> > char character;
>> > fooclass *next;
>> > public:
>> > void init(int setnum, char setchar, int quantity) {
>> > number = setnum;
>> > character = setchar;
>> > if(quantity>0){
>> > next = new(fooclass);
>> > next.init(setnum, setchar, quantity-1);
>> > }//end if
>> > }//end init method
>> > };//end class
>>
>> I don't understand. How can initialization be recursive when initialization
>> occurs once?
>
>But a linked list is itself a recursive data structure. You can
>initialize the entire list by initializing the first element, then
>initializing the list consisting of everything after the head of the
>first list. This is an "elegant" solution but not a particularly
>efficient one.
>
>>
>> Try this:
>> fooclass(int setnum = 0, /* or a default value */
>> char setchar = 0, /* or a default value */
>> quantity = 0)
>> : number(setnum), character(setchar), next(NULL)
>> {
>> }
>>
>> I don't see how your init function relates to a linked list. Is it creating
>> another node? another list? inserting an element?
>>
>Try tracing through the code with a relatively small value for
>"quantity". Or go out and learn LISP.
>
>Seth Jones
- Raw text -