From: -Gadget-nospam- AT vic DOT bigpond DOT net DOT au (Gadget) Newsgroups: comp.lang.c++,comp.os.msdos.djgpp,comp.programming Subject: Re: Question: Linked lists and classes: Self initiation Date: Sat, 24 Apr 1999 02:51:07 GMT Organization: Telstra Big Pond Cable - Sydney Site 1 Lines: 62 Message-ID: <37213049.1471831@news-server> References: <3720BE52 DOT 6E86A8C4 AT xoommail DOT com> <3720D21C DOT 9617323B AT atlantis DOT stortek DOT com> NNTP-Posting-Host: 24.192.25.58 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: m2.c2.telstra-mm.net.au 924922267 16224 24.192.25.58 (24 Apr 1999 02:51:07 GMT) X-Complaints-To: news AT m2 DOT c2 DOT telstra-mm DOT net DOT au NNTP-Posting-Date: 24 Apr 1999 02:51:07 GMT X-Newsreader: Forte Agent 1.5/32.451 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com 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