(Latest Revision: Thu Oct 26 15:16:44 PDT 2000)
SOURCE FILE: ListT.cpp
#include <stddef.h> // for NULL
#include <assert.h> // for assert
/* START NODE DEFINITIONS */
template <class T>
struct listNode
{
T Item;
listNode<T>* Next;
listNode(); //constructor
}; // end struct
template <class T>
listNode<T>::listNode(): Next(NULL)
{
} // end default constructor
/* END NODE DEFINITIONS */
/* START LISTCLASS DEFINITIONS */
/* ################################################## */
/* Default Constructor */
/* ################################################## */
template <class T>
listClass<T>::listClass(): Size(0), Head(NULL)
{
} // end default CONSTRUCTOR
/* ################################################## */
/* Copy Constructor */
/* ################################################## */
template <class T>
listClass<T>::listClass(const listClass<T>& L): Size(L.Size)
{
CopyListNodes(L) ;
} // end copy constructor
/* ################################################## */
/* Destructor */
/* ################################################## */
template <class T>
listClass<T>::~listClass()
{
MakeListEmpty() ;
} // end destructor
/* ################################################## */
/* MakeListEmpty */
/* ################################################## */
template <class T>
void listClass<T>::MakeListEmpty()
{
bool Success;
while (!ListIsEmpty())
ListDelete(1, Success);
} // end destructor
/* ################################################## */
/* ListIsEmpty */
/* ################################################## */
template <class T>
bool listClass<T>::ListIsEmpty() const
{
return bool (Size == 0) ;
}
/* ################################################## */
/* ListLength */
/* ################################################## */
template <class T>
int listClass<T>::ListLength() const
{
return Size ;
}
/* ################################################## */
/* operator= */
/* ################################################## */
template <class T>
listClass<T>& listClass<T>::operator=(const listClass<T>& Rhs)
{
// check for assignment to self
if (this != &Rhs)
{ MakeListEmpty(); // deallocate left-hand side
CopyListNodes(Rhs); // copy list nodes
Size = Rhs.Size; // copy size of list
} // end if
return *this;
} // end operator=
/* ################################################## */
/* ListInsert */
/* ################################################## */
template <class T>
void listClass<T>::ListInsert(int NewPosition, T NewItem,
bool& Success)
{
int NewLength = ListLength() + 1;
Success = bool( (NewPosition >= 1) && (NewPosition <= NewLength) );
if (Success)
{ Size = NewLength;
// create new node and place NewItem in it
listNode<T>* NewPtr = new listNode<T>;
Success = bool(NewPtr != NULL);
if (Success)
{ NewPtr->Item = NewItem;
// attach new node to list
if (NewPosition == 1)
{ // insert new node at beginning of list
NewPtr->Next = Head;
Head = NewPtr;
}
else
{ listNode<T>* Prev = PtrTo(NewPosition-1);
// insert new node after node
// to which Prev points
NewPtr->Next = Prev->Next;
Prev->Next = NewPtr;
} // end if
} // end if
} // end if
} // end ListInsert
/* ################################################## */
/* ListDelete */
/* ################################################## */
template <class T>
void listClass<T>::ListDelete(int Position, bool& Success)
{
listNode<T>* Cur;
Success = bool( (Position >= 1) &&
(Position <= ListLength()) );
if (Success)
{ --Size;
if (Position == 1)
{ // delete the first node from the list
Cur = Head; // save pointer to node
Head = Head->Next;
}
else
{ listNode<T>* Prev = PtrTo(Position-1);
// delete the node after the
// node to which Prev points
Cur = Prev->Next; // save pointer to node
Prev->Next = Cur->Next;
} // end if
// return node to system
Cur->Next = NULL;
delete Cur;
Cur = NULL;
} // end if
} // end ListDelete
/* ################################################## */
/* ListRetrieve */
/* ################################################## */
template <class T>
void listClass<T>::ListRetrieve (int Position,
T& DataItem,
bool& Success) const
{
Success = bool( (Position >= 1) && (Position <= ListLength()) );
if (Success)
{ // get pointer to node, then data in node
listNode<T>* Cur = PtrTo(Position);
DataItem = Cur->Item;
} // end if
} // end ListRetrieve
/* ################################################## */
/* PtrTo */
/* ################################################## */
template <class T>
listNode<T>* listClass<T>::PtrTo(int Position) const
{
if ( (Position < 1) || (Position > ListLength()) )
return NULL;
else // count from the beginning of the list
{ listNode<T>* Trav = Head;
for (int Skip = 1; Skip < Position; ++Skip)
Trav = Trav->Next;
return Trav;
}
} // end PtrTo
/* ################################################## */
/* CopyListNodes */
/* ################################################## */
template <class T>
void listClass<T>::CopyListNodes(const listClass<T> & L)
{
if (L.Head == NULL)
Head = NULL; // original list is empty
else
{ // copy first node
Head = new listNode<T>;
assert(Head != NULL); // check allocation
Head->Item = L.Head->Item;
// copy rest of list
listNode<T>* NewPtr = Head; // new list pointer
// NewPtr points to last node in new list
// OrigPtr points to nodes in original list
for (listNode<T>* OrigPtr = L.Head->Next;
OrigPtr != NULL;
OrigPtr = OrigPtr->Next)
{ NewPtr->Next = new listNode<T>;
assert(NewPtr->Next != NULL);
NewPtr = NewPtr->Next;
NewPtr->Item = OrigPtr->Item;
} // end for
NewPtr->Next = NULL;
} // end if
}
/* END LISTCLASS DEFINITIONS */