Mail Archives: djgpp/1999/04/24/08:03:54
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 -