Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Deprecated/RefList/LADTTEX.C

Go to the documentation of this file.
00001 const int LIST_SIZE = 10;
00002 
00003 template <class Elem>
00004 class List {                // Array-based list class
00005 private:
00006   int msize;                // Maximum size of list
00007   int numinlist;            // Actual number of Elems in list
00008   int curr;                 // Position of "current" Elem
00009   Elem* listarray;          // Array holding list Elems
00010 public:
00011   List(int =LIST_SIZE);     // Constructor
00012   ~List();                  // Destructor
00013   void clear();             // Remove all Elems from list
00014   void insert(const Elem);  // Insert Elem at current position
00015   void append(const Elem);  // Insert Elem at tail of list
00016   Elem remove();            // Remove and return current Elem
00017   void setFirst();          // Set curr to first position
00018   void prev();              // Move curr to previous position
00019   void next();              // Move curr to next position
00020   int  length() const;      // Return current length of list
00021   void setPos(int);         // Set curr to specified position
00022   void setValue(const Elem); // Set current Elem's value
00023   Elem currValue() const;   // Return current Elem's value
00024   bool isEmpty() const;     // Return TRUE if list is empty
00025   bool isInList() const;    // TRUE if curr is within list
00026   bool find(int);           // Find value (from current pos)
00027 };
00028 
00029 template <class Elem>
00030 List<Elem>::List(int sz) {  // Constructor: initialize
00031   msize = sz; numinlist = curr = 0;
00032   listarray = new Elem[sz];
00033 }
00034 
00035 template <class Elem>
00036 List<Elem>::~List()         // Destructor: return array space
00037   { delete [] listarray; }
00038 
00039 template <class Elem>
00040 void List<Elem>::clear()    // Remove all Elems from list
00041   { numinlist = curr = 0; } // Simply reinitialize values
00042 
00043 // Insert Elem at current position
00044 template <class Elem>
00045 void List<Elem>::insert(const Elem item) {
00046   // Array must not be full and curr must be a legal position
00047   assert((numinlist < msize) && (curr >=0)
00048                              && (curr <= numinlist));
00049   for(int i=numinlist; i>curr; i--) // Shift up to make room
00050     listarray[i] = listarray[i-1];
00051   listarray[curr] = item;
00052   numinlist++;              // Increment current list size
00053 }
00054 
00055 template <class Elem>
00056 void List<Elem>::append(const Elem item) { // Insert at tail
00057   assert(numinlist < msize);  // List must not be full
00058   listarray[numinlist++] = item; // Increment list size
00059 }
00060 
00061 template <class Elem>
00062 Elem List<Elem>::remove() { // Remove and return current Elem
00063   assert(!isEmpty() && isInList()); // Must be Elem to remove
00064   Elem temp = listarray[curr];        // Store removed Elem
00065   for(int i=curr; i<numinlist-1; i++) // Shift elements down
00066     listarray[i] = listarray[i+1];
00067   numinlist--;              // Decrement current list size
00068   return temp;
00069 }
00070 
00071 template <class Elem>
00072 void List<Elem>::setFirst()      // Set curr to first position
00073   { curr = 0; }
00074 
00075 template <class Elem>
00076 void List<Elem>::prev() { curr--; } // Move to previous pos
00077 
00078 template <class Elem>
00079 void List<Elem>::next() { curr++; } // Move to next position
00080 
00081 template <class Elem>
00082 int List<Elem>::length() const      // Return length of list
00083   { return numinlist; }
00084 
00085 template <class Elem>
00086 void List<Elem>::setPos(int pos)    // Set to specified pos
00087   {curr = pos; }
00088 
00089 
00090 int main() { // Create and use a list variable
00091   List<int> L1(15);
00092   List<double> L2;
00093 
00094   L1.append(12);
00095   L2.append(12.0);
00096 }

Generated on Sun Oct 14 18:46:30 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001