www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/24/08:03:54

From: seth AT kansmen DOT com (Seth Jones)
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp,comp.programming
Subject: Re: Question: Linked lists and classes: Self initiation
Message-ID: <MPG.118aa8dd6a37fb039896f6@client.sw.news.psi.net>
References: <3720BE52 DOT 6E86A8C4 AT xoommail DOT com> <3720D21C DOT 9617323B AT atlantis DOT stortek DOT com>
Organization: Kansmen
X-Newsreader: MicroPlanet Gravity v2.10
Lines: 48
Date: Fri, 23 Apr 1999 23:57:49 GMT
NNTP-Posting-Host: 38.170.72.106
NNTP-Posting-Date: Fri, 23 Apr 1999 19:57:49 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019