You are here:

C++/Multiple double link list

Advertisement


Question
I have to create a large number of double link list. Is there a way to create these in a function or Algorithm or a loop, or just create each link list separately? I can create a link list, or a double link list, but I can not find anything on creating multiple link list.

Thanks.
Ed

Answer
If you replace double linked list with say int and linked list with say primitive in your question so it read:

   "I have to create a large number of ints. Is there a way to create these in
    a function or Algorithm or a loop, or just create each primitive separately?
    I can create a primitive, or an int, but I can not find anything on creating
    multiple primitives."

Would you still have a problem?

Basically presuming your linked list or doubly linked list classes allow instances to be created in the usual ways then you create them as and when required, as you would with any other object (be they primitives such as ints, chars, floats or class types such as a linked list).

So let us presume you are using the doubly linked list provided by the C++ standard library. In fact this is a class template and you provide it with the element type to create specific doubly linked list classes.

The code fragments below show using the std::list class template to create a specialisation (class) to hold int values, and various ways we can create instances of them. Note that these are code fragments only and not complete programs and are for example use only and are in no way supposed to be meant as production quality. The do of course presume you are using a reasonably modern C++  implementation (i.e. late 1990s or newer) that has a reasonable implementation of the ISO C++ standard library.

   #include <list>

   typedef std::list<int>  ListOfInt;      // type alias for a std::list type that contains int elements.

   ...

   ListOfInt aListOfInt;
   ListOfInt anotherListOfInt;

   unsigned int const ArraySize(100);

   ListOfInt arrayOfListOfInt[ArraySize];             // creates ArraySize lists
   
   ListOfInt * arrayOfPointersToListOfInt[ArraySize]; // creates ArraySize _pointers_ to lists...

   for ( unsigned int i=0; i<ArraySize; ++i )         // .. so have to create lists for them to point to...
   {
       arrayOfPointersToListOfInt[i] = new ListOfInt;
   }

   ...

   for ( unsigned int i=0; i<ArraySize; ++i )         // ... and later remember to delete them
   {                                                  //     as they were created dynamically
       delete arrayOfPointersToListOfInt[i];
   }

We can even have a list of list of whatever in which each element is itself a list. For example following on from the previous examples we might want a list containing lists that contain ints:

   typedef std::list<int>        ListOfInt;
   typedef std::list<ListOfInt>  ListOfListOfInt;

   ...

   ListOfListOfInt  aListOfListOfInt;

Of course we can use other containers to hold lists - built in arrays for example, as in the original example. Maybe using a std::vector instead of a plain built in array:

   #include <list>
   #include <vector>

   typedef std::list<int>          ListOfInt;
   typedef std::vector<ListOfInt>  VectorOfListOfInt;

   ...

   VectorOfListOfInt aVectorOfListOfInt;

Or perhaps a mapping of strings to a list of values (although we could get a similar effect using a C++ library multimap type):

   #include <list>
   #include <map>
   #include <string>

   typedef std::list<int>                    ListOfInt;
   typedef std::map<std::string, ListOfInt>  MapStringToListOfInt;

   ...

   MapStringToListOfInt aMapStringToListOfInt;


So it is really up to you and your requirements as to how you use these building blocks and how exactly you put them together.

Hope this has given you some ideas.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.