linklist.h

Go to the documentation of this file.
00001 //**************************************************************************/
00002 // Copyright (c) 1998-2006 Autodesk, Inc.
00003 // All rights reserved.
00004 // 
00005 // These coded instructions, statements, and computer programs contain
00006 // unpublished proprietary information written by Autodesk, Inc., and are
00007 // protected by Federal copyright law. They may not be disclosed to third
00008 // parties or copied or duplicated in any form, in whole or in part, without
00009 // the prior written consent of Autodesk, Inc.
00010 //**************************************************************************/
00011 // FILE:        linklist.h
00012 // DESCRIPTION: Linked-list template classes
00013 // AUTHOR:      Tom Hudson
00014 // HISTORY:     created 10 December 1995
00015 //**************************************************************************/
00016 #pragma once
00017 
00018 #include "maxheap.h"
00019 
00020 template <class T> class LinkedEntryT: public MaxHeapOperators {
00021 public:
00022     T data;
00023     void* next;
00024     LinkedEntryT(T& d) { data = d; next = NULL; }
00025 };
00026 
00033 template <class T, class TE> class LinkedListT: public MaxHeapOperators
00034 {
00035 private:
00036     TE* head;
00037     TE* tail;
00038     int count;
00039 public:
00042     LinkedListT()
00043     {
00044         head = tail = NULL;
00045         count = 0;
00046     }
00048     ~LinkedListT()
00049     {
00050         New();
00051     }
00054     void New()
00055     {
00056         while(head)
00057         {
00058             TE* next = (TE*)head->next;
00059             delete head;
00060             head = next;
00061         }
00062         head = tail = NULL;
00063         count = 0;
00064     }
00066     int         Count() { return count; }
00073     void Append(T& item)
00074     {
00075         TE* entry = new TE(item);
00076         if(tail)
00077             tail->next = entry;
00078         tail = entry;
00079         if(!head)
00080             head = entry;
00081         count++;
00082     }
00088     T& operator[](int index)
00089     {
00090         TE* e = head;
00091         while(index && e) {
00092             e = (TE*)e->next;
00093             index--;
00094         }
00095         // This should never happen, so we'll punt and return...
00096         // the head's data
00097         if(!e) {
00098             DbgAssert(0);
00099             return head->data;
00100         }
00101         return e->data;
00102     }
00108     LinkedListT& operator=(LinkedListT &from)
00109     {
00110         New();
00111         for(int i = 0; i < from.Count(); ++i)
00112             Append(from[i]);
00113         return *this;
00114     }
00115 };