Click here to Skip to main content
6,630,289 members and growing! (15,935 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Intermediate

A linked list collection class in MC++

By sultan_of_6string

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.
C++/CLI, VC7, Windows, .NET 1.0, Dev
Posted:20 Jun 2002
Updated:24 Jun 2002
Views:189,856
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.90 Rating: 3.15 out of 5
1 vote, 100.0%
1

2

3

4

5

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


Member

Location: United States United States

Other popular C++ / CLI articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
Generalhmmm.... Pinmembersultan_of_6string19:25 21 Jun '02  
GeneralRe: hmmm.... PinmemberChristian Graus20:37 21 Jun '02  
GeneralRe: hmmm.... PinmemberRama Krishna2:57 22 Jun '02  
GeneralRe: hmmm.... PinmemberChristian Graus3:21 22 Jun '02  
GeneralRe: hmmm.... PineditorHeath Stewart6:26 9 Jul '03  
GeneralProblems that are not very obvious PinmemberRama Krishna17:37 21 Jun '02  
GeneralRe: Problems that are not very obvious Pinmemberthe guy who wrote it and is too lazy to log on19:26 22 Jun '02  
GeneralArrayList is actually efficient PinmemberOz Solomonovich17:16 21 Jun '02  
GeneralRe: ArrayList is actually efficient PinmemberChristian Graus17:43 21 Jun '02  
GeneralRe: ArrayList is actually efficient PinmemberJérémie Chassaing6:58 24 Jun '02  
GeneralStandard Template Library PinmemberRodLima8:20 21 Jun '02  
GeneralRe: Standard Template Library PinmemberWilliam E. Kempf8:56 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus16:28 21 Jun '02  
GeneralRe: Standard Template Library Pinmembersultan_of_6string16:32 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus16:41 21 Jun '02  
GeneralRe: Standard Template Library Pinmembersultan_of_6string16:49 21 Jun '02  
GeneralRe: Standard Template Library PinmemberJörgen Sigvardsson0:21 9 Aug '02  
GeneralRe: Standard Template Library PinmemberNishant S16:52 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus16:55 21 Jun '02  
GeneralRe: Standard Template Library PinmemberRicku16:59 21 Jun '02  
GeneralRe: Standard Template Library PinmemberNishant S17:00 21 Jun '02  
GeneralRe: Standard Template Library PinmemberRama Krishna17:17 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus17:46 21 Jun '02  
GeneralRe: Standard Template Library PinmemberNishant S19:48 21 Jun '02  
GeneralRe: Standard Template Library PinmemberChristian Graus17:40 21 Jun '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2002
Editor: Nishant Sivakumar
Copyright 2002 by sultan_of_6string
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project