1 /****************************************************
7 ****************************************************/
12 const int MAX_SIZE = 35;
18 // list node definition
25 Node *head; // the head of the list
26 Node *tail; // the tail of the list
27 Node *curr; // the current position in the list
28 int num_items; // the number of items in the list
32 // remember that an empty list has a "size" of -1 and its "position" is at -1
42 // clones the list l and sets the last element as the current
49 // clones the list l and sets the last element as the current
50 void operator=(const List& l)
66 // navigates to the beginning of the list
72 // navigates to the end of the list
73 // the end of the list is at the last valid item in the list
79 // navigates to the specified element (0-index)
80 // this should not be possible for an empty list
81 // this should not be possible for invalid positions
84 if(!IsEmpty() && pos >= 0 && pos < GetSize())
95 // navigates to the previous element
96 // this should not be possible for an empty list
97 // there should be no wrap-around
104 while(n->link != curr)
111 // navigates to the next element
112 // this should not be possible for an empty list
113 // there should be no wrap-around
116 if(!IsEmpty() && curr != tail)
120 // returns the location of the current element (or -1)
138 // returns the value of the current element (or -1)
147 // returns the size of the list
148 // size does not imply capacity
154 // inserts an item before the current element
155 // the new element becomes the current
156 // this should not be possible for a full list
157 void InsertBefore(T data)
185 // inserts an item after the current element
186 // the new element becomes the current
187 // this should not be possible for a full list
188 void InsertAfter(T data)
210 n->link = curr->link;
220 // removes the current element (collapsing the list)
221 // this should not be possible for an empty list
239 else if(curr == tail)
248 curr->link = curr->link->link;
257 // replaces the value of the current element with the specified value
258 // this should not be possible for an empty list
259 void Replace(int data)
265 // returns if the list is empty
268 return (num_items == 0);
271 // returns if the list is full
274 return (num_items == MAX_SIZE);
277 // returns the concatenation of two lists
278 // l should not be modified
279 // l should be concatenated to the end of *this
280 // the returned list should not exceed MAX_SIZE elements
281 // the last element of the new list is the current
282 List operator+(const List& l) const
284 List newList = *this;
290 newList.InsertAfter(n->data);
297 // returns if two lists are equal (by value)
298 bool operator==(const List& l) const
300 if(num_items != l.num_items)
308 if(n->data != p->data)
318 // returns if two lists are not equal (by value)
319 bool operator!=(const List& l) const
321 return !(*this == l);
324 // returns a string representation of the entire list (e.g., 1 2 3 4 5)
325 // the string "NULL" should be returned for an empty list
326 friend ostream& operator<<(ostream& out, const List &l)
336 out << n->data;// << " ";