Sender: root AT mail1 DOT remote DOT uva DOT nl Message-ID: <372206EC.FE35FFBD@mail.uva.nl> Date: Sat, 24 Apr 1999 20:01:16 +0200 From: Johan Blok X-Mailer: Mozilla 4.07 [en] (X11; I; Linux 2.0.36 i686) MIME-Version: 1.0 To: djgpp AT delorie DOT com Subject: Re: Question: Linked lists and classes: Self initiation References: <3720BE52 DOT 6E86A8C4 AT xoommail DOT com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com > Consider the implementation of a linked list as I programmed and use it, it deletes itself recursively. It's easy to implement a recursively initiate method. I have used several implementations of linked list and like this one, because it's simple, because of classdesign you have to use it efficiently. Code: // Begin 13 april, class JBDList, class implementing a linked list. #ifndef JBDLIST_H #define JBDLIST_H template class JBDList { private: TYPE Data; JBDList *Next, *I; public: JBDList(const TYPE AData) { Next=NULL; Data=AData; } virtual ~JBDList() { if (Next!=NULL) delete Next; } // Direct get/setters TYPE& GetData() { return Data; } void SetData(TYPE AData) { Data=AData; } JBDList *GetNext() const { return Next; } void SetNext(JBDList *ANext){ Next=ANext; } //Indirect get/setters TYPE& GetData(long AIndex) { return Get(AIndex)->GetData(); } JBDList *Get(long AIndex); long GetCount(); //Add and remove routines JBDList *AddNext(TYPE AData); JBDList *Add(TYPE AData, long AIndex) { return (Get(AIndex-1)->AddNext(AData)); } JBDList *AddLast(TYPE AData); void DelNext(); void Del(long AIndex) { return Get(AIndex-1)->DelNext(); } void Clear(); // Iterate routines JBDList *IReset() { I=Next; return I; } JBDList *INext() { I=I->GetNext(); return I; } JBDList *GetI() { return I; } TYPE& GetIData() { return I->GetData(); } }; template JBDList *JBDList::Get(long AIndex) { JBDList *Work; Work=this; for (long i=0; iGetNext(); } return Work; } template JBDList *JBDList::AddNext(TYPE AData) { if (Next==NULL) { Next=new JBDList(AData); return Next; } else { JBDList *Work; Work=Next; Next=new JBDList(AData); Next->SetNext(Work); return Next; } } template JBDList *JBDList::AddLast(TYPE AData) { JBDList *Work; Work=this; while (Work->GetNext()!=NULL) { Work=Work->GetNext(); } return Work->AddNext(AData); } template void JBDList::DelNext() { if (Next!=NULL) { JBDList *Work; Work=Next->GetNext(); Next->SetNext(NULL); delete Next; Next=Work; } } template void JBDList::Clear() { if (Next!=NULL) delete Next; Next=NULL; } template long JBDList::GetCount() { long Count=1; JBDList *Work; Work=this; while (Work->GetNext()!=NULL) { Work=Work->GetNext(); Count++; } return Count; } #endif > Ishpeck wrote in message <3720BE52 DOT 6E86A8C4 AT xoommail DOT com>... > >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