LinkedListTemplate v1.2
           Documentation

Welcome to LinkedListTemplate v1.2, A freeware "Linked List" class to simplify the use of linked lists.

 

 

Contents

 

 

 

Features

 

 

 

History

v1.0 (13th December 2000)

v1.1 (21st January 2001)

v1.1 (23rd January 2001)

v1.2 (5th January 2002)

 

 

 

LinkedListTemplate Class Members

Add

Adds an item to the list.

Remove

Removes an item from the list.

Clear

Clears the whole list.

Item

Returns the data of an item.

NextItem

Returns the data of the next item.

PrevItem

Returns the data of the previous item.

Swap

Swaps two items.

Length

Returns the total number of elements in the list.

Find

Finds the position of an item in the list.

operator[]

Returns the data of an item.

 

 

 

Add

bool List <T>::Add ( long InsertAt, bool InsertBefore)
bool List <T>::Add (T ItemData, long InsertAt, bool InsertBefore)

Remarks

Adds an item to the list.  'ItemData' contains the data to add, where T is the type of data. If you don't specify 'ItemData', an empty element will be inserted.  'InsertAt' specifies the item index; if 'InsertBefore' is 'true', then the new item will be placed just before the item with index 'InsertAt', else, it will be placed just after this item.  The last two parameters are optional.  If omitted, the item will be placed after the last item. The function will return false, whenever you specify an invalid index for 'InsertAt'.  To add after the last item, set 'InsertAt' to -1.  This is not really necessary, because 'InsertAt' is set to -1 by default.

Note: The indices of the items count up from 0.

See Also Remove, Example

 

 

Remove

bool List <T>::Remove (long ItemIndex)

Remarks

Removes the item at position 'ItemIndex'.  If 'ItemIndex' is '0', then the first item will be removed. The function will return false, whenever you specify an invalid index for 'ItemIndex'.

Note: The indices of the items count up from 0.  So if 'ItemIndex' is '0', then the first item in the list will be removed.  To remove the last item, you should set 'ItemIndex' to the length of the list minus 1 (Length() - 1).

See Also Add, Clear, Example

 

 

Clear

void List <T>::Clear ()

Remarks

Simply clears the list.  After clearing, the list will be in its initial state (like it was just after creating the list). 

See Also Example

 

 

Item

T List <T>::Item (long ItemIndex);

Remarks

Returns the item at index 'ItemIndex'.  The returned type will be T.  For more information about T, read the example. The function will return NULL, if 'ItemIndex' is an invalid index.

Note: The indices of the items count up from 0.

See Also operator [], NextItem, Swap, Example

 

 

NextItem

T List <T>::NextItem ();

Remarks

Returns the next item.  The returned type will be T.  For more information about T, read the example. The function will return an empty T (0, if T is an int, or a structure filled with all 0's if T is a struct, etc.), if there is no next item.  The next item will be next from the item you last accessed (the last call to Item, operator[], NextItem, PrevItem or Remove affects this).  NextItem was implemented to improve performance of the Linked List in loops.  See the example for more detail.

Note: The indices of the items count up from 0.
Caution: NextItem returns an empty T (0, if T is an int, or 0.0 if T is a double etc.)! This may lead to confusion when T is anything else than a pointer! 

See Also Item, operator [], PrevItem, Example

 

 

PrevItem

T List <T>::PrevItem ();

Remarks

Returns the previous item.  The returned type will be T.  For more information about T, read the example. The function will return an empty T (0, if T is an int, or a structure filled with all 0's if T is a struct, etc.), if there is no previous  item.  The previous item will be previous from the item you last accessed (the last call to Item, operator[], NextItem, PrevItem or Remove affects this).  PrevItem was implemented to improve performance of the Linked List in loops.  See the example for more detail.

Note: The indices of the items count up from 0.
Caution: PrevItem returns an empty T (0, if T is an int, or 0.0 if T is a double etc.)! This may lead to confusion when T is anything else than a pointer! 

See Also Item, operator [], NextItem, Example

 

 

Swap

bool  List <T>::Swap (long ItemOne, long ItemTwo);

Remarks

Swap two elements in the list. 'ItemOne' and 'ItemTwo' specify the indices of the items to swap.  If either of the two parameters is an invalid index, the function will return false.

Note: The indices of the items count up from 0.

See Also Item, operator [], NextItem, Example

 

 

Length

long List <T>::Length ();

Remarks

Returns the total number of items in the list.

See Also Item, Example

 

 

Find

long List <T>::Find (T FindItem, long StartAt);

Remarks

Returns the first occurrence of 'FindItem' in the list, starting at 'StartAt'.  If the item can not be found, it will return '-1'.

See Also Item, Example

 

 

operator []

T  List <T>::operator [] (long ItemIndex);

Remarks

Serves as a shortcut for the member 'Item'.  See the example for details

See Also Item, Example

 

 

Example

[...]

List <double> list_d;                     // creates a list whose data type will be double.
                                          // Now, every 'T' in the code will be interpreted as 'double'

list_d.Add(1.11);
list_d.Add(2.22);
list_d.Add(3.33);

double myDouble1 = list_d.Item(2);        // myDouble1 will contain 3.33 (of type double, of course)
list_d.Swap(0, 2);
double myDouble2 = list_d.Item(2);        // myDouble2 will contain 1.11 (of type double, of course)

list_d.Remove(2);

long list_length = list_d.Length();       // list_length will contain 2, because we removed the third item

list_d.Clear();                           // now, we start all over
list_d.Add(1.01);
double myDouble3 = list_d[0];             // myDouble 3 = 1.01 -- also demonstrates the operator []

long item_index = list_d.Find(1.11, 0);   // item_index = -1, because the item was not found
long item_index = list_d.Find(1.01, 0);   // item_index = 0 */
double item = list_d[0];
for (int i = 0; i < list_d.Length(); i++)
{
    [ ... do something with 'item' ... ]
    item = list_d.NextItem();             // Get the next item.  Same as calling list_d[i] in the
}                                         // end, but a lot faster!

list_d.Add(2.22);                         // add some more items
list_d.Add(3.33);

double item = list_d[2];
for (int i = list_d.Length(); i > 0; i--)
{
    [ ... do something with 'item' ... ]
    item = list_d.PrevItem();             // Get the next item.  Same as calling list_d[i] in the
}                                         // end, but a lot faster!

[...]

 

 

 

 

Download the Source

The link below will froward you to PlanetSourceCode.com, where I uploaded the source code. I would be glad if you could vote for this code at PlanetSourceCode or maybe leave a comment. Thank you.

Download

 

 

 

Contacting the Author

Jakob Bieling
Email: me at jakobbieling dot de
ICQ: 9612338
Web: http://code.jakobbieling.de

Please report any bugs you have found!  Thanks.

5th January 2002