LINKED LISTS

An alternative to arrays, linked lists rely heavily on pointers and dynamically allocated memory. Unlike arrays, storage of linked lists need not be contiguous.

The idea: for each data item, store both the data and a pointer to the next data item (and the next pointer).

For the specification of the StringList class using linked lists, see string-list.h. For the implementation see string-list.C.

Adding items to linked lists (in general)

  1. Create a new list item:
    temp = new ListItem
        
  2. Set the value of that item to be whatever we are inserting:
    temp->val = entry
        
  3. `Swing' the appropriate pointers so that the order is maintained. Suppose we are inserting a new list item C between A and B:
    A->next = C;      // A->next swings from B to C
    C->prev = A;     
    B->prev = C;      // B->prev swings from A to C
    C->next = B;