Professional Documents
Culture Documents
by VB.NET language elements. For example, you can read The Useful Generic List in
VB.NET to learn more about one of the higher level ways to uses lists in VB.NET.
But you can code a list "the hard way" too. This article shows one way to do that for a
double linked list. But unlike other articles you might see, we're going to approach the
solution with a lot of explanation because understanding this example is a great way to
make sure you understand what's really happening in your code.
A linked list is simply an array of structures where each element contains a "pointer" to the
next one. This basic structure underlies many of the collections and arrays in most
programming languages - in concept at least. (The actual code that Microsoft uses ... well ...
) And in many languages such as C++, it continues to be implemented in code.
In any case, understanding how one works genuinely qualifies as a fundamental concept.
The simplest linked list is a single linked list. Here's a logical diagram of one:
--------
Click Here to display the illustration
--------
The arrows are the "pointer" values, that is "links", and they're addresses of some type. A
language like C++ will give you direct access to memory addresses that you can use in your
code that are called pointers. In Visual Basic, pointers are handled for you. (In VB.NET,
you can use a Delegate and the AddressOf function which is the equivelent to a pointer in
C++ more or less.) You can learn about Delegate in the article, Using Delegates in Visual
Basic .NET for Runtime Flexibility.
The node value is the information "payload" in the list. Note that these nodes don't have to
be kept together in memory. Since a node always points to the next one, you can move
them around and change the links to modify the list.
For example, the links let you insert or remove individual nodes by simply changing where
the links point.
If the last node points to the first one, then it's called a circular linked list. And if each node
has a pointer to both the next node and the previous node, then it's called a double linked
list.
--------
Click Here to display the illustration
--------
Since VB.NET doesn't support memory pointers directly, one way to accomplish something
similar is illustrated in the (somewhat dated now) book Visual Basic Design Patterns in the
chapter on interface and abstract classes. The authors provide this code to accomplish the
job. (This version has been cleaned up just a bit to make it easier to understand, and I also
added a property to hold one data item, ElementContent.)
When I looked at this code, I said to myself, "Gee! I'll just whip out some code to use this
class." It turned out to be easier said than done. I hate it when that happens! It would have
been nice if the authors had included a more complete sample. (You can see more sample
code in the article The Abstract Base Class.)
This is a really interesting piece of code. Notice that the properties of the Interface
IDoubleLink are themselves IDoubleLink types. This allows a sort of recursive data
structure that is easier to code than you might think:
--------
Click Here to display the illustration
--------
I did it a couple of times by mistake while writing the example code to use theElement.
Although code like this ...
thisElement.NextElement.PreviousElement = thisElement
... turned out to have some real debugging headaches, the eventual code that accomplishes a
double linked list looks like this:
The property ElementContent is used in this code to "keep track" of which node is which.
(Otherwise, it's really hard to tell because nothing in VB.NET helps tell which node is
which.) That value is assigned only once when nodes are added ...
thisElement.ElementContent = theList.Count
If you trace through the code a few times while you add and delete nodes, you will see that
the relative location of the nodes (in theList) jumps all over the place, but the double links
keep track of where the previous and next nodes are. And, I wrote this myself for this
article ... so there could be bugs! If you find any, please let me know! Enjoy!