home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 21
/
CD_ASCQ_21_040595.iso
/
dos
/
graphic
/
font3d11
/
f3d_src
/
list.h
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-09
|
5KB
|
149 lines
//===========================================================================================================
// List.H
//
// Copyright (c) 1994 by Todd A. Prater
// All rights reserved.
//
//-----------------------------------------------------------------------------------------------------------
//
// A simple linked-list class. All operations are defined as 'inline', but 'Empty' and 'Add' might not
// be expanded, depending on the compiler.
//
// Operations: LIST(void)..............................Creates an empty list.
// Empty(void).............................Empties a list by deleting all its objects.
// Add(T*).................................Adds an object to the list. The new object should
// already be created before the call to 'Add' is
// made.
// Count(void).............................Returns the number of objects in the list.
// First(void).............................Returns a pointer to the first object in the list.
// Current(void)...........................Returns a pointer to the current object in the
// list.
// Last(void)..............................Returns a pointer to the last object in the list.
// Next(void)..............................Returns a pointer to the next object in the list.
// gotoFirst...............................Moves the window over the first object in the list.
// gotoNext................................Moves the window over the next object in the list.
// gotoLast................................Moves the window over the last object in the list.
//
//
//===========================================================================================================
#ifndef __List_H__
#define __List_H__
#include <stdlib.h>
#include <iostream.h>
#include <stddef.h>
#include "Config.H"
template <class T>
class LISTLINK
{
private: T* obj;
LISTLINK<T>* next;
public: LISTLINK (void)
{
obj = NULL;
next = NULL;
}
LISTLINK (T* object, LISTLINK<T>* tonext)
{
obj = object;
next = tonext;
}
T* Obj (void) { return obj; };
LISTLINK<T>* Next (void) { return next; };
void setObj (T* object) { obj = object; };
void setNext (LISTLINK<T>* n) { next = n; };
};
template <class T>
class LIST
{
private: INT count;
LISTLINK<T>* first;
LISTLINK<T>* current;
LISTLINK<T>* last;
public: LIST(void)
{
count = 0;
first = NULL;
current = NULL;
last = NULL;
}
void Empty()
{
INT i;
LISTLINK<T>* tempnext;
gotoFirst();
for (i=0;i<count;i++)
{
delete current->Obj();
gotoNext();
}
gotoFirst();
for (i=0;i<count;i++)
{
tempnext = current->Next();
if (tempnext!=NULL) delete current;
current = tempnext;
}
count=0;
first=NULL;
current=NULL;
last=NULL;
}
BYTE Add (T* object)
{
LISTLINK<T>* newlink;
newlink = new LISTLINK<T>(object,NULL);
if (!newlink)
{
cout << "ERROR: Out of Memory in LIST";
abort();
}
if (count==0)
{
first = newlink;
current = newlink;
last = newlink;
last->setNext(NULL);
}
else
{
last->setNext(newlink);
last = newlink;
}
count++;
return (TRUE);
};
ULONG Count (void) { return count; };
T* First (void) { return first->Obj(); };
T* Current (void) { return current->Obj(); };
T* Last (void) { return last->Obj(); };
T* Next (void) { return current->Next()->Obj(); };
void gotoFirst (void) { current = first; };
void gotoLast (void) { current = last; };
void gotoNext (void) { if (current==last) return;
current = current->Next(); };
};
#endif