The SequentialList Class uses the generic pointer technique with
typedef void* Pointer
Thus, items in a sequential list can be pointers to any object, with a generic pointer used to access the object. In derived classes, like StringList, this generic pointer is converted to a specific type of pointer, like const char*. The methods are summarized in table Class SequentialList.
An important point to keep in mind when using a SequentialList is that its destructor does not delete the elements in the list. It would not be possible to do so, since it has only a generic pointer. Also, note that random access (by element number, or any other method) can be very inefficient, since it would require sequentially chaining down the list.
SequentialList has an iterator class called ListIter. The ++ operator (or next member function) returns a Pointer.
Method |
Description |
---|---|
void append (Pointer p) |
add the element p to the end of the list |
Pointer elem (int n) |
return the n-th element in the list (zero if there are fewer than n) |
int empty () |
return 1 if empty, 0 if not |
Pointer getAndRemove () |
return and remove the first element in the list (return zero if empty) |
Pointer getTailAndRemove () |
return and remove the last element in the list (return zero if empty) |
Pointer head () |
return the first element in the list (zero if empty) |
void initialize () |
remove all elements from the list |
int member (Pointer p) |
return 1 if the list has a pointer equal to p, 0 if not |
void prepend (Pointer p) |
add the element p to the beginning of the list |
int remove (Pointer p) |
if the list has a pointer equal to p, remove it and return 1; 0 if not |
int size () |
return the number of elements in the list |
Pointer tail () |
return the last element in the list (zero if empty) |