Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #ifndef LIST_H 00010 #define LIST_H 00011 00012 #include "Adt.h" 00013 #include "Exceptions.h" 00014 00015 namespace grafalgo { 00016 00029 class List : public Adt { 00030 public: List(int=26); 00031 ~List(); 00032 00033 void clear(); 00034 void resize(int); 00035 void expand(int); 00036 00037 void copyFrom(const List&); 00038 00039 // methods to access items 00040 index get(position) const; 00041 index next(index) const; 00042 index first() const; 00043 index last() const; 00044 int length() const; 00045 00046 // predicates 00047 bool valid(index) const; 00048 bool empty() const; 00049 bool member(index) const; 00050 bool equals(List&) const; 00051 bool isConsistent() const; 00052 00053 // modifiers 00054 virtual bool insert(index,index); 00055 virtual bool removeNext(index); 00056 bool addFirst(index); 00057 bool addLast(index); 00058 bool removeFirst(); 00059 00060 // input/output 00061 string& toString(string&) const; 00062 friend istream& operator>>(istream&, List&); 00063 00064 protected: 00065 // managing dynamic storage 00066 void makeSpace(int); 00067 void freeSpace(); 00068 00069 private: 00070 int len; 00071 index head; 00072 index tail; 00073 index *nxt; 00074 }; 00075 00080 inline index List::next(index i) const { return nxt[i]; } 00081 00085 inline index List::first() const { return head; } 00086 00090 inline index List::last() const { return tail; } 00091 00096 inline bool List::valid(index i) const { return 1 <= i && i <= n(); } 00097 00101 inline bool List::empty() const { return first() == 0; } 00102 00106 inline int List::length() const { return len; } 00107 00112 inline bool List::member(index i) const { 00113 if (!valid(i)) { 00114 stringstream ss; ss << "get(" << i << ")"; 00115 string s = ss.str(); 00116 throw IllegalArgumentException(s); 00117 } 00118 return nxt[i] != -1; 00119 } 00120 00125 inline bool List::addFirst(index i) { return insert(i,0); } 00126 00131 inline bool List::addLast(index i) { return insert(i,last()); } 00132 00136 inline bool List::removeFirst() { return removeNext(0); } 00137 00138 } // ending namespace 00139 00140 #endif 00141