Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

list.h

Go to the documentation of this file.
00001 
00002 #ifndef LIST_H
00003 #define LIST_H
00004 
00005 
00006 /*===============================<o>=====================================
00007 
00008 Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International,
00009 www.bearcave.com.
00010 
00011 All Rights Reserved
00012 
00013 You may use this software in software components for which you do
00014 not collect money (e.g., non-commercial software).  All commercial
00015 use is reserved.
00016 
00017 ===============================<o>=====================================*/
00018 
00019 
00035 template <class T>
00036 class LIST
00037 {
00038 public:
00039     typedef struct LIST_TYPE {
00040         T data;
00041         struct LIST_TYPE *next;
00042     } list_type;  // struct list_type
00043 
00044 private:
00045   list_type *list;
00046 
00047 public:
00048   typedef list_type *handle;
00049 
00050 public:
00051   LIST(void) 
00052   { 
00053     list = 0;
00054   }
00055 
00056 
00058   void dealloc(void)
00059   {
00060     while ( remove() != 0 )
00061       /* nada */;
00062   } // dealloc
00063 
00064 
00065   void add( T data )
00066   {
00067     list_type *t;
00068     
00069     t = new list_type;
00070     t->data = data;
00071     t->next = 0;
00072     if (list == 0) {
00073       list = t;
00074     }
00075     else {
00076       t->next = list;
00077       list = t;
00078     }
00079   }  // add
00080 
00081 
00082   // reverse the list
00083   void reverse(void)
00084   {
00085     list_type *elem, *prev, *next;
00086 
00087     prev = 0;
00088     next = 0;
00089 
00090     for (elem = list; elem != 0; prev = elem, elem = next) {
00091       next = elem->next;
00092       elem->next = prev;
00093     } // for 
00094     list = prev;
00095   }  // reverse
00096 
00097 
00099   unsigned int length(void)
00100   {
00101       list_type *elem;
00102       unsigned int cnt = 0;
00103 
00104       for (elem = list; elem != 0; elem = elem->next)
00105           cnt++;
00106       return cnt;
00107   }  // lenght
00108 
00114   handle remove(void)
00115   {
00116     list_type *t;
00117 
00118     if (list != 0) {
00119       t = list;
00120       list = t->next;
00121       delete t;
00122     }
00123     return list;
00124   } // remove
00125 
00126 
00128   T get_item( handle h)
00129   {
00130 
00131     return h->data;
00132   } // get_item
00133 
00134 
00136   handle first(void)
00137   {
00138     return list;
00139   } // first
00140 
00141 
00143   handle next(handle h)  
00144   {
00145     list_type *next = 0;
00146 
00147     if (h != 0) {
00148         next = h->next;
00149     }
00150 
00151     return next;
00152   } // next
00153   
00154 };  // template class LIST
00155 
00156 
00157 #endif
00158 

Generated on Wed Mar 31 21:15:55 2004 for Data Structures for a VHDL Compiler by doxygen 1.3.3