delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/01/15:39:15

From: "Nils Öster" <nils DOT oster AT swipnet DOT se>
Newsgroups: comp.os.msdos.djgpp
Subject: Linked lists problem
Lines: 112
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Message-ID: <Gx6J3.221$kE1.267@nntpserver.swip.net>
NNTP-Posting-Host: 212.151.56.206
X-Complaints-To: news-abuse AT swip DOT net
X-Trace: nntpserver.swip.net 938800358 212.151.56.206 (Fri, 01 Oct 1999 19:52:38 MET DST)
NNTP-Posting-Date: Fri, 01 Oct 1999 19:52:38 MET DST
Organization: A Customer of Tele2
X-Sender: s-699593 AT d212-151-56-206 DOT swipnet DOT se
Date: Fri, 1 Oct 1999 19:52:46 +0200
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

My question is:How do I integrate a dynamical linked list system into
another class?

let me show you some code to illustrate my problem:

class COORDINATE;

COORDINATE *coordinate; //GLOBAL variable
                                                 //this is the actual
problem, if I dont want this to be a global variable.



//Here comes the definition:

class COORDINATE
{
    public:
    //object data
    int x,y;
   //linked list stuff:
    COORDINATE *prev, *next;

//constructor
    COORDINATE(int a, int b)
    {

        x=a;
        y=b;

        prev=NULL;
        next=coordinate;
        next->prev=this;
        coordinate=this;
     }

//destructor
    ~COORDINATE()
    {
        x=0;
        y=0;

        if (prev!=NULL) prev->next=next;
        else
        coordinate=next;        //if this line is executed it means that the
global "coordinate" was deleted

   }
}


int main()
{
    //Now, the only thing i have todo to create a new COORDINATE object is
for example:

    new COORDINATE(5,5);
    new COORDINATE(6,7);
    new COORDINATE(3,5);



    //for example now mabey I want to delete all objects that has  y set to
5

    COORDINATE *save_next,*p=coordinate;

    while(p!=NULL)
    {
        save_next=p->next;
        if (p->y==5) delete p;

        p=save_next;
    }





}

----------------------------------------------------------------------------
--------------------------------------------------------------------------


All of this code should work, but what if would want to have the
"coordinate" inside another class eg.:

class STARFIELD
{
    COORDINATE *star_coordinates
     // followed by some other stuff
       .
       .
       .
}


STARFIELD starfield;

----------------------------------------------------------------------------
--------------------------------------------------------------------------

//then I would have to make a new "COORDINATE" class for the STARFIELD class
where all references to "coordinate" is exchanged with
"starfield->star_coordinates" and this would be very bad code.

How do I solve this?




- Raw text -


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