Click here to Skip to main content
Licence 
First Posted 20 Jun 2002
Views 209,960
Bookmarked 17 times

A linked list collection class in MC++

By | 24 Jun 2002 | Article
The .NET ArrayList class provides "dynamic arrays" which, to a C++ programmer should seem really innane. Here's a linked list collection class that can be used in any .NET language.

Introduction

If you examine the functionality of the ArrayList class, you'll find that it's quite ridiculous. When the array reaches maximum capacity, it creates a new array with double the capacity, copies the items from the old array into the new one and discards the old array. Convoluted and messy.

Every C/C++ programmer knows that the only way to make structures that are 'really' dynamic is to use linked lists. I've created a class in Managed C++ that does just that. Furthermore, I've included a TypedListBase abstract class from which you can inherit to make strongly typed collections. All this is put into a .NET dll and I've a program, written in C# that I used to test it.

The classes really don't do anything more than implement the IList, ICollection, IEnumerable and ICloneable. Therefore, it behaves identically to ArrayList as far as the programmer who's using it is concerned. I'm not getting into the details of the methods and properties of these interfaces here, but they're pretty straight-forward and self-explanatory, and the .NET Framework reference explains them quite well.

Implementing LinkedList

The LinkedList class maintains a linked list of LinkedListNode structures shown below:

__nogc struct LinkedListNode
{
    LinkedListNode __nogc* pNext;
    LinkedListNode __nogc* pPrev;
    gcroot<object*> item; // #include <vcclr.h> in StdAfx.h  
                          // makes this template available.
};

LinkedListNode::item stores the objects. The gcroot template is required to hold a managed object in an unmanaged struct/class.

The linked list class includes the following items, besides a constructor, destructor and the interface implementations:

public __gc __sealed class LinkedList : public IList, 
    public IEnumerable, public ICollection, public ICloneable
{
// Implementation
private:
    LinkedListNode __nogc* m_pHead;
    // Future versions will support reverse enumeration, 
    // like the STL classes.
    // Therefore, we need to store the tail as well.
    LinkedListNode __nogc* m_pTail;
    int m_count;
    // Used by the enumerator to check 
    // whether the list is modified after the
    // enumerator has been created.
    int m_version;	

    LinkedListNode* GetNodeAt(int index);
public:
    int GetVersion();

. . .
. . .

Implementing the enumerator

The code for LinkedList::GetEnumerator is:

IEnumerator* LinkedList::GetEnumerator()
{
    return new LinkedListEnumerator(this, m_pHead);
}

This class requires a pointer to the list it's working on and a pointer to the head. The class declaration is given below:

private  __gc __sealed class LinkedListEnumerator : public IEnumerator
{
// Implementation
private:
    LinkedList* m_list;
    LinkedListNode* m_pNode;
    LinkedListNode* m_pHead;
    int m_version;
public:
    LinkedListEnumerator(LinkedList* list, LinkedListNode* pHead);
    // Overrides of IEnumerator
public:
    __property Object* get_Current();
    bool MoveNext();
    void Reset();
};

The constructor calls list->GetVersion() and stores the return value in m_version. This number represents the state of the list when the enumerator is created. When the list is modified (by calling Add, Remove, Clear, etc.) the version is incremented. The IEnumerator functions check to make sure that the stored version and the current version are the same:

if (m_version != m_list->GetVersion())
    throw new InvalidOperationException(
        S"The list was modified after the enumerator was created.");

If the list is modified after the enumerator is created, there is a chance that LinkedListEnumerator::m_pNode and m_pHead are invalid, so the exception is thrown. This is really stretching it, but if the list is modified over 4 billion times (I think that's the capacity of an int), there will be an overflow. Well, this is how Microsoft implemented this behavior in ArrayList.

Testing the DLL with the program

Check out the program I've included. It has a GUI and lets you add strings to the list one by one and can add 500 random strings as well. It tests all the functions and lets you enumerate through the list using multiply threads. Try adding items while some threads are enumerating. They will throw InvalidOperationException, but an enumerator created after the items were added will run 'peacefully.'

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

sultan_of_6string



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhmmm.... Pinmembersultan_of_6string18:25 21 Jun '02  
GeneralRe: hmmm.... PinmemberChristian Graus19:37 21 Jun '02  
So the .NET runtime does not offer a list structure ? That is LAME !!!
 
I'd expect you'll fund your list takes longer to iterate through but is quicker to delete/insert into Smile | :)

 
Christian
 
I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
 
Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
GeneralRe: hmmm.... PinmemberRama Krishna1:57 22 Jun '02  
GeneralRe: hmmm.... PinmemberChristian Graus2:21 22 Jun '02  
GeneralRe: hmmm.... PineditorHeath Stewart5:26 9 Jul '03  
GeneralProblems that are not very obvious PinmemberRama Krishna16:37 21 Jun '02  
GeneralRe: Problems that are not very obvious Pinmemberthe guy who wrote it and is too lazy to log on18:26 22 Jun '02  
GeneralArrayList is actually efficient PinmemberOz Solomonovich16:16 21 Jun '02  
GeneralRe: ArrayList is actually efficient PinmemberChristian Graus16:43 21 Jun '02  
GeneralRe: ArrayList is actually efficient PinmemberJérémie Chassaing5:58 24 Jun '02  
GeneralStandard Template Library PinmemberRodLima7:20 21 Jun '02  
GeneralRe: Standard Template Library PinmemberWilliam E. Kempf7:56 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus15:28 21 Jun '02  
GeneralRe: Standard Template Library Pinmembersultan_of_6string15:32 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus15:41 21 Jun '02  
GeneralRe: Standard Template Library Pinmembersultan_of_6string15:49 21 Jun '02  
GeneralRe: Standard Template Library PinmemberJörgen Sigvardsson23:21 8 Aug '02  
GeneralRe: Standard Template Library PinmemberNishant S15:52 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus15:55 21 Jun '02  
GeneralRe: Standard Template Library PinmemberRicku15:59 21 Jun '02  
GeneralRe: Standard Template Library PinmemberNishant S16:00 21 Jun '02  
GeneralRe: Standard Template Library PinmemberRama Krishna16:17 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus16:46 21 Jun '02  
GeneralRe: Standard Template Library PinmemberNishant S18:48 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus16:40 21 Jun '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 25 Jun 2002
Article Copyright 2002 by sultan_of_6string
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid