Top Up Prev Next Bottom Contents Index Search

1.5 Class SequentialList


This class implements a single linked list with a count of the number of elements. The constructor produces a properly initialized empty list, and the destructor deletes the links. However, the destructor does not delete the items that have been added to the list; this is not possible because it has only void * pointers and would not know how to delete the items. There is an associated iterator class for SequentialList called ListIter.

1.5.1 SequentialList information functions

These functions return information about the SequentialList but do not modify it.

int size() const;
Return the size of the list.

Pointer head() const; 
Return the first item from the list (0 if the list is empty). The list is not changed.

Pointer tail() const; 
Return the last item from the list (0 if the list is empty). The list is not changed.

Pointer elem(int n) const; 
Return the nth item on the list (0 if there are fewer than n items). Note that the time required is proportional to n.

int empty() const; 
Return 1 if the list is empty, 0 if it is not.

int member(Pointer arg) const; 
Return 1 if the list has a Pointer that is equal to arg, 0 if not.

1.5.2 Functions that modify a SequentialList

void prepend(Pointer p); 
Add an item at the beginning of the list.

void append(Pointer p); 
Add an item at the end of the list.

int remove(Pointer p); 
Remove the pointer p from the list if it is present (the test is pointer equality). Return 1 if present, 0 if not.

Pointer getAndRemove(); 
Return and remove the head of the list. If the list is empty, return a null pointer (0).

Pointer getTailAndRemove(); 
Return and remove the last item on the list.

void initialize(); 
Remove all links from the list. This does not delete the items pointed to by the pointers that were on the list.

1.5.3 Class ListIter

ListIter is a standard iterator class for use with objects of class SequentialList. The constructor takes an argument of type const SequentialList and the ++ operator (or next function) returns a Pointer. Class ListIter is a friend of class SequentialList. In addition to the standard iterator functions next and reset, this class also provides a function

void reconnect(const SequentialList& newList)

that attaches the ListIter to a different SequentialList.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.