Lists
Sequence ADT
- Ordered collection of homogenous elements
- Can be constructed and destroyed
- Can insert and remove elements (at a particular position)
- Can be cleared (emptied)
- Can be traversed (front to back)
- Has a number of elements (which can be accessed)
- Can be compared for equality, inequality, and lex order
- Can be copied and assigned
Implementation as list template class
- Doubly linked list -- each element has pointer to previous and next
- Constant-time insert: need only change four pointers
inserting C between A and B
A's next points to C
B's previous points to C
C's previous points to A
C's next points to B- Constant-time remove: need only change two pointers
removing B between A and C
A's next points to C
C's previous points to A- No random-access (no [ ], no at( ) function like for vectors)
would need to traverse entire list to get to particular position- Uses iterators to implement traversal
begin( ) points to first element
end( ) points to one-past-last element
can use like pointers- Also front( ) and back( ) for first and last element
Constructors
- list < T > L( ); // new empty list of type T
- list < T > L( int n); // new list with n elements of type T; each default constructed
- list < T > L(int n, T x); // new list with n elements of type T; each initialized to x
- list < T > L(first, last); // new list containing elements from first through last
- also copy constructor, destructor
List Operations
- insert ( p, x ); // insert element x before pointer/iterator p
- insert ( p, n, x ); // insert n copies of element x before p
- insert ( p, first, last ); // insert elements [first, last] before p
- erase (p); // remove element at pointer/iterator p
- erase ( first, last ); // remove elements [first, last]
- clear ( ); // empty the list
- push_front ( ); pop_front ( ); // Insert or remove at beginning
- push_back ( ); pop_back ( ); // Insert or remove at end
Generic Operations
- size ( ); // returns number of elements
- empty ( ); // returns "Is list empty?"
Splicing
- insert ( p, first, last ) must copy all elements between first, last
- splicing inserts (any part of) an actual list into another list
- splice ( list :: iterator pos, list& l); // move all elements from l to before pos in this
- splice ( list :: iterator pos, list& l, list :: iterator p); // move elements after p from x
into this before pos- splicing does no copying; elements are removed from original list and inserted into new list
- void merge ( list& l ); // merge two sorted lists, l and this
// l is left empty; this contains elements of both lists (ordered)
// preserves relative order of equivalent elements
Example
list < int > intlist ( 47 , 8); // list of 47 ints; each initialized to 8
list < Rational > ratlist ( 50, Rational (2, 3) ); // list of 50 Rationals; each initialized to 2/3
list < float > floatlist ( 20 ); // list of 20 floats; each initialized to 0.0
list :: iterator ibegin = intlist.begin( );
list :: iterator iend = intlist.end( );
floatlist.insert ( floatlist.begin( ), ibegin, iend); //
inserts entire intlist at beginning of floatlist
// each int 8 is converted to float 8.0
ratlist.push_back( intlist.front( ) ); // inserts
first element of intlist at back of ratlist
// int 8 converted to Rational 8/1 (default constructor argument)
ratlist.insert ( ratlist.end ( ), 3, 4 ); // inserts 3 copies of Rational 4/1 at end of ratlist
intlist.erase ( intlist.first( ) + 1 , intlist.last( ) ); // remove all but first element of intlist
Rational r = ratlist.pop_front ( ); // remove and return first element of ratlist
Rational* ratarray = new Rational[ ratlist.size( ) ]; // want to copy ratlist into an array
int index = 0;
for( list :: iterator i = ratlist.begin( ) ; i !=
ratlist.end( ) ; i ++ ) // use iterator to traverse
ratarray [ index++ ] = *i;