The List ADT
Definition
A List is a dynamic, ordered tuple of homogeneous elements
A1,
A2,
A3,
.... An where Ai is the i'th element of the List
- Dynamic
- Ordered
- Homogeneous
Definition:
The position of an element Ai is i.
Positions range from 1 to N, inclusive, NOT 0 to N - 1
Definition:
The size of a List is N.
A List with no elements is called an empty list.
List Operations
- List ( ) -- construct an empty list
- List (const List & rhs) -- construct a list as a copy of rhs
- ~List ( ) -- destroy a List
- const List & operator= ( const List & rhs ) -- make the list contain copies of the elements of rhs in the same order
- isEmpty ( ) -- returns TRUE if the list size is zero
- makeEmpty ( ) -- causes the list to become empty
- remove ( const Object x ) -- removes the first occurrence of x if present. If not present, the list is unchanged
- insert, find, findPrevious -- later
Iterators
Definition:
An iterator is an object that provides access to elements
of a collection (in a specified order) without
exposing the underlying structure of the collection.
- the order is dictated by the iterator
- the collection provides iterators "on demand"
- each iterator on a collection is independent
- iterator operations are generic
Iterator Operations
- bool isPastEnd ( ) -- returns TRUE if the iterator is past the end of the List
- void advance ( ) -- advances the iterator to the next position in the list. If the iterator is already past end, no change.
- const Object & retrieve ( ) -- returns the element in the list at the current position of the iterator.
Scanning a collection
Iterator itr = collection.first ( );
for ( ; ! itr.isPastEnd ( ); itr.advance ( ) )
// do something with itr.retrieve
More List Operations
- ListItr <Object> zeroth ( ) -- returns an iterator that represents the element before the first element
- ListItr <Object> first ( ) -- returns an iterator representing the first element of the list
- ListItr <Object> find (const Object & x) -- returns an iterator representing the first occurrence of x in the list
- ListItr<Object> findPrevious (const Object & x ) -- returns an iterator representing the element before x in the list
- void insert (const Object & x, const ListItr<Object> & p ) -- inserts a copy of x in the list after the element referred to by p