C++ Templated Linked-List Class Example I am tired of messing with the STL List class with all the strange sytax like (&(*object)).function() and those iterators to keep track of. So, I decided to write my own. I had a miserable time finding a tutorial on how to build a linked-list that uses templates (but plenty that hold just ints - wheee!) so here is my own. First thing's first: List Node Class
Even before we can design a list node class, we have to quickly visit some issues that come up. Since I can't think of a way to pass parameters to the constructor of the object stored in the node class, it will have to define a default, parameterless constructor. (You have to do the same thing with the STL list class.) When an object is added, the assignment operator is called - and unless the templated class overrides it, a simple bitwise copy of the object is performed. This is fine for most classes but if it those classes allocate any memory on the heap, pointers to that memory are copied - not the contents of that memory.
All the bits having to do with templates are highlighted in blue. It was helpful to me to think of it just like a macro expansion that you would #define. After saying tempate <class T>, use T (or whatever name you like) wherever you would normally place the variable type. Also notice that when referring to templated classes, the brackets (< >) and templated class essentially become part of the name.
List Class I want to make the interface to the list as simple as possible. We need functions to do these things: So here's a basic class outline.
This seems to be the most efficient interface for accessing the list - in code, it looks like: list.start(); // Reset the list to the beginning while (list.next()) { // Iterate through all the elements list.get().storedClassFunction(); // Get the elements }The list is internally arranged so that it starts with a dummy node with 'NULL' for the pointer to the next node, and each added node is essentially inserted between the dummy and the 'NULL' so that the node at the end of the list always points to 'NULL'. The dummy node is needed for the remove operation - for consistancy, the remove() always causes the currently accessible element to be removed and reset to the previous element so calling next() after remove() does not skip any elements. If you want to remove something at the beginning of the list, you have a problem - hence, the dummy. This arrangement is quite simple, but has the side effect of listing elements backwards from the order they were added. There's also no way of sorting, but neither of these things particularly matter to me. (If you want things sorted - you'll have to overload the class to keep sorted's < operator and add the code to insert in order in the add() function.) Implementation
Add
Remove
Next
Get
Destructor
start() simply points the current and lastCurrent pointer to the dummy headNode. The complete source is available below. Source Use
Sample.cpp
Output
Source The code was compiled and tested with Visual C++ 6.0 - it should work with other compilers but I haven't tested it. If you decide to use it, best of luck to you. It could probably benefit from a function to clear the list - just duplicate the destructor code. My Code Section |