An ordered collection of elements where each element,
except for the first has an immediate predicessor,
and each element, except for the last has an immediate
successor.
An empty list is possible and is a list containing no elements.
Often charactarized by constant time insertion and deletion.
Operations include
First, last, next, previous
insert, delete
find
Performance characteristics:
We expect everything but find to be O(1)
We expect find to be linear.
Implementation
Normally associated with a link and a data field (singly linked lists)
Or a data field, a forward link and a backward link. (Doubly linked lists)
Normally a pointer to the first (head) and last (tail) of the
list are maintained.
In some implementations, an empty node represents a singly linked list.
In some doubly linked applications, an empty head and tail node
are kept
Usage
Lists are normally employed when there is a need to maintain
order and
There will be many insertions and deletions.
And there is not a major need for random access.
Consider, for example if we wish to delete every other
item in the structure. O(n2) in an array, O(n) in a
list.
The STL list class.
#include <list>
This implements a doubly linked list.
list<type> identifier
Operations:
push_back, push_front
pop_front, pop_back
bool empty, int size
clear removes all items
remove(item), removes all instances of the item
front, back returns first or last item in the list
sort sorts the list.
= is supported
Iterators:
list<type>::iterator id
list<type>::reverse_iterator id
begin() returns a forward iterator to the first item
end() returns a forward iterator to one past the last item