From: bena AT NOSPAMsirplc DOT co DOT uk (Ben Ashley) Newsgroups: comp.os.msdos.djgpp Subject: Re: Linked lists and other examples Date: Mon, 05 Jan 1998 10:03:41 GMT Organization: Areti Internet Public News Service Lines: 110 Message-ID: <34b0a673.1673683@news.areti.co.uk> References: <9712191317 DOT AA13835 AT sun4nl DOT NL DOT net> <34a1a5e4 DOT 8777871 AT news DOT areti DOT co DOT uk> <68dit6$nd0$1 AT cadmium DOT aware DOT nl> Reply-To: bena AT sirplcNOSPAM DOT co DOT uk NNTP-Posting-Host: 194.130.222.18 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Tue, 30 Dec 1997 13:54:51 GMT, authentic AT tip DOT nl (Rick) wrote: >Ben, > >While you are at it, I like very much to ask a few questions >about your double linked lists, since I am doing a few linked >list classes for exercise too. First of all what are the specifics >for djgpp in this case ?? Secondly you are talking about >'passive' and 'active' iterators, and I don't know the meaning >of those concepts. Are you talking about iterators that >return a reference and can be used as a l-value as well as >a r-value ?? (Like fi mylist[2]=myitem). Or am I missing something?? As Mr Aldritch mentioned, The specifics for DJGPP is that indeed, that your linked list can be larger than one wrote in a non-protected mode compiler, such as Borland. If written well though, a linked list can be compiled on any compiler providing it is only using very basic C/C++, which luckily for us, a linked list does. The reason I use the term 'basic' is that when dealing with template classes, things get a little more interesting and different compilers handle them differently. As for 'passive' and 'active' iterators, here is an example of the two. In these examples, BList is a linked list class. It has a function called NewIterator() which returns an iterator for the particular instance of BList on which it was called, and NewActiveIterator(). void ActiveFunction( void* pItem ) { char* pString = ( char* )pItem; cout << pString << endl; }; int main( void ) { // Some chars that will be populated in to the list... char* pC1 = "String 1"; char* pC2 = "String 2"; char* pC3 = "String 3"; BList mylist; // Populate linked list with some stuff.... mylist.Add( ( void* )pC1 ); mylist.Add( ( void* )pC2 ); mylist.Add( ( void* )pC3 ); // Use a passive iterator to display the strings to the user // Note that we must delete it afterwards... BIterator* pIterator = mylist.NewIterator(); char *pRunner = ( char* )pIterator->First(); while( pRunner != NULL ) { cout << pRunner << endl; pRunner = pIterator->Next(); }; delete( pIterator ); // Use an active iterator to display the strings to the user // called just like NewIterator() except we pass it the function // that we wish to get called. BActiveIterator* pActiveIterator; pActiveIterator = mylist.NewActiveIterator( ActiveFunction ); pActiveIterator->Run(); delete( pActiveIterator ); }; Although you can easily write linked lists in C, it gets much better when using C++. In the above example, you see a BList class which can create an iterator for itself. The example is pretty simple, but a more complex collection class library would have BList deriving from a base class such as BColl... an BIterator might become the base class and we might derive BListIterator from that. This means that you can very easily copy one collection of one type to another ( IE, a linked list in to an array ), and also iterate over a collection without knowing what type of collection it is ( Your abstract NewIterator() function returns a pointer to a base class Iterator ). > >Also have you looked at David Nugent, Auke Reitsma's sources >in the snippets, DETL sources from Doug Eleveld ? Any opinion on them I haven't as yet, but I will take a look!! Cheers, MooBen Programmer for some people I can't quite remember "Winnie says he's *Not* In-Animate"