Introduction
As promised in Single Link List article, here is the implementation of Double Link List, in thread safe way. This article is quite similar to the one I've already mentioned.
You might find it useful to your applications.
Using the code
First, add everything that's in the pack to your project:
- LinkedListDouble.h
- LinkedListDouble.cpp
- NamedCriticalSection.h
- NamedCriticalSection.cpp
- SpinLock.h
- SpinLock.cpp
- DebugTrace.h
- DebugTrace.cpp
Second, include this where you need to declare a new list:
#include "LinkedListDouble.h"
#include "LinkedListDouble.cpp"
Once you did all this, declare a new list, like this:
CLinkedListDouble<int, sinzeof(int)> IntegerList;
The template can be used with anything... so, let's complicate the things a bit. Let's say we have this class:
class SomeClass
{
double DVar;
public:
char szString[ 32 ];
SomeClass(): DVar( 0 ) { memset( szString, 0, 32 ); };
~SomeClass(){};
void Increase( void ){ DVar++; };
}
Now, you want a list of SomeClass
elements. You declare it like this:
CLinkedListDouble<SomeClass, sinzeof(SomeClass)> SomeClassList;
CLinkedListDouble
It's not a complex class... once you get to know it :).
template <class T, int i>
class CLinkedListDouble
{
CDebugTrace Trace;
USHORT Size;
public:
CLinkedListDoubleNode< T, i > *pHead;
CLinkedListDoubleNode< T, i > *pTail;
CNamedCriticalSection Section;
CLinkedListDouble();
~CLinkedListDouble();
bool Initialize();
bool Free();
bool Add(T&);
bool Remove(T&);
bool Remove(USHORT);
bool Insert(USHORT, T&);
USHORT GetSize(void);
};
Normally, this may not be of your interest. This class is made to just "set it and use it".
One more thing. If you ever need to directly access the elements of the list - let's say you need to "walk" through all the elements and do something - remember to do this:
CLinkedListDouble<SomeClass, sinzeof(SomeClass)> SomeClassList;
CSpinLock Spin( &(SomeClass.Section), "NoWhere" );
if( Spin.Lock() )
{
...
Spin.Unlock();
}
Points of Interest
Anything involving double linked lists.
One more thing...
I'm not interested in ratings, edited article or any article competition. I never took in consideration any ratings when reading an article or downloading. I will not bother to answer messages like: "There is already an article like this here, and it's better...".
I just post here because CodeProject really helped me over the years, so I want to give back something. If something like this already exists here, on CodeProject... I do not really care. Use the one you want - it's your choice.
Anyway... I'll continue to post here!
History