delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/25/06:10:15

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 <J DOT Blok AT mail1 DOT remote DOT uva DOT nl>
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> <tQ3U2.99$95 DOT 5156 AT news2 DOT giganews DOT com>
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 TYPE>
class JBDList
{
  private:
    TYPE Data;
    JBDList<TYPE> *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<TYPE> *GetNext() const { return Next; }
    void SetNext(JBDList<TYPE> *ANext){ Next=ANext; }

    //Indirect get/setters
    TYPE& GetData(long AIndex) { return Get(AIndex)->GetData(); }
    JBDList<TYPE> *Get(long AIndex);
    long GetCount();

    //Add and remove routines
    JBDList<TYPE> *AddNext(TYPE AData);
    JBDList<TYPE> *Add(TYPE AData, long AIndex) { return
(Get(AIndex-1)->AddNext(AData)); }
    JBDList<TYPE> *AddLast(TYPE AData);
    void DelNext();
    void Del(long AIndex) { return Get(AIndex-1)->DelNext(); }
    void Clear();

    // Iterate routines
    JBDList<TYPE> *IReset() { I=Next; return I; }
    JBDList<TYPE> *INext() { I=I->GetNext(); return I; }
    JBDList<TYPE> *GetI() { return I; }
    TYPE& GetIData() { return I->GetData(); }
};

template<class TYPE>
JBDList<TYPE> *JBDList<TYPE>::Get(long AIndex)
{
  JBDList<TYPE> *Work;
  Work=this;
  for (long i=0; i<AIndex; i++)
  {
    Work=Work->GetNext();
  }
  return Work;
}

template<class TYPE>
JBDList<TYPE> *JBDList<TYPE>::AddNext(TYPE AData)
{
  if (Next==NULL)
  {
    Next=new JBDList<TYPE>(AData);
    return Next;
  }
  else
  {
    JBDList<TYPE> *Work;
    Work=Next;
    Next=new JBDList<TYPE>(AData);
    Next->SetNext(Work);
    return Next;
  }
}

template<class TYPE>
JBDList<TYPE> *JBDList<TYPE>::AddLast(TYPE AData)
{
  JBDList<TYPE> *Work;
  Work=this;
  while (Work->GetNext()!=NULL)
  {
    Work=Work->GetNext();
  }
  return Work->AddNext(AData);
}

template<class TYPE>
void JBDList<TYPE>::DelNext()
{
  if (Next!=NULL)
  {
    JBDList<TYPE> *Work;
    Work=Next->GetNext();
    Next->SetNext(NULL);
    delete Next;
    Next=Work;
  }
}

template<class TYPE>
void JBDList<TYPE>::Clear()
{
  if (Next!=NULL) delete Next;
  Next=NULL;
}

template<class TYPE>
long JBDList<TYPE>::GetCount()
{
  long Count=1;
  JBDList<TYPE> *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


- Raw text -


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