Public Member Functions

LinkedListT< T, TE > Class Template Reference

Search for all occurrences

Detailed Description

template<class T, class TE>
class LinkedListT< T, TE >

Description:
Simple linked list class. Methods and operators are provided to create new linked lists, return the number of items in the list, access item using the array operator ([]), and assign one list to another. All methods of this class are implemented by the system.

#include <linklist.h>

Inheritance diagram for LinkedListT< T, TE >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

  LinkedListT ()
  ~LinkedListT ()
void  New ()
int  Count ()
void  Append (T &item)
T &  operator[] (int index)
LinkedListT operator= (LinkedListT &from)

Constructor & Destructor Documentation

LinkedListT ( ) [inline]
Remarks:
Constructor. The list is initialed to NULL and the count is set to 0.
    {
        head = tail = NULL;
        count = 0;
    }
~LinkedListT ( ) [inline]
Remarks:
Destructor.
    {
        New();
    }

Member Function Documentation

void New ( ) [inline]
Remarks:
The items in the list are deleted. The list is initialed to NULL and the count is set to 0.
    {
        while(head)
        {
            TE* next = (TE*)head->next;
            delete head;
            head = next;
        }
        head = tail = NULL;
        count = 0;
    }
int Count ( ) [inline]
Remarks:
Returns the number of items in the list.
{ return count; }
void Append ( T &  item ) [inline]
Remarks:
Adds a new item to the end of the list.
Parameters:
T& item

The item to add.
Operators:
    {
        TE* entry = new TE(item);
        if(tail)
            tail->next = entry;
        tail = entry;
        if(!head)
            head = entry;
        count++;
    }
T& operator[] ( int  index ) [inline]
Remarks:
Allows access to items in the list using the array operator. The first item in the list has an index of 0.
Parameters:
int index

The array index of the item to access.
    {
        TE* e = head;
        while(index && e) {
            e = (TE*)e->next;
            index--;
        }
        // This should never happen, so we'll punt and return...
        // the head's data
        if(!e) {
            DbgAssert(0);
            return head->data;
        }
        return e->data;
    }
LinkedListT& operator= ( LinkedListT< T, TE > &  from ) [inline]
Remarks:
Assignment operator.
Parameters:
LinkedListT &from

The list to copy.
Returns:
A new linked list that is a copy of the list passed.
    {
        New();
        for(int i = 0; i < from.Count(); ++i)
            Append(from[i]);
        return *this;
    }