2.Lists§

A list is a finite, ordered sequence of data items.

Important concept: List elements have a position.

Notation: \(<a_0, a_1, …, a_{n-1}>\)

What operations should we implement?

List Implementation Concepts

Our list implementation will support the concept of a current position.

Operations will act relative to the current position.

\(<20, 23\ |\ 12, 15>\)

List ADT (1)

List ADT (2)

List ADT (3)

List ADT Examples

List: \(<12\ |\ 32, 15>\)

L.insert(99);

Result: \(<12\ |\ 99, 32, 15>\)

Iterate through the whole list:

for (L.moveToStart(); !L.isAtEnd(); L.next()) {
  it = L.getValue();
  doSomething(it);
}

List Find Function

// Return true if k is in list L, false otherwise
static boolean find(List<Integer> L, int k) {
  for (L.moveToStart(); !L.isAtEnd(); L.next()) {
    if (k == L.getValue()) {
      return true; // Found k
    }
  }
  return false;                         // k not found
}

Array-Based List Class (1)

class AList<E> implements List<E> {
  private E listArray[];                  // Array holding list elements
  private static final int DEFAULT_SIZE = 10; // Default size
  private int maxSize;                    // Maximum size of list
  private int listSize;                   // Current # of list items
  private int curr;                       // Position of current element

Array-Based List Insert

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Linked List Position (1)

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Linked List Position (2)

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Linked List Position (3)

We will add list header and list trailer nodes. This eliminates all the special cases.


Design Principle: Design to Avoid Special Cases

Adding list header/trailer nodes add a little space and (simple) code to the list class constructor.
However, adding them avoids dealing with special cases that potentially involve bug-prone code
Avoids writing code for most special cases when inserting into empty list, at head of list, or at end of list.
Avoids writing code for most special cases when deleting first, last, or only element in list.

Linked List Class (1)

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Linked List Class (2)

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Insertion

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Removal

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Prev

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Overhead

Comparison of Implementations

Array-Based Lists:
Insertion and deletion are \(\Theta(n)\).
Prev and direct access are \(\Theta(1)\).
Array must be allocated in advance.
No overhead if all array positions are full.
Linked Lists:
Insertion and deletion are \(\Theta(1)\).
Prev and direct access are \(\Theta(n)\).
Space grows with number of elements.
Every element requires overhead.

Space Comparison

“Break-even” point:

\(DE = n(P + E)\)

\(n = \frac{DE}{P + E}\)

E: Space for data value.

P: Space for pointer.

D: Number of elements in array.

Space Example

Freelist

System new and garbage collection are slow.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Doubly Linked Lists

Doubly Linked Node (1)

class Link<E> {         // Doubly linked list node
  private E e;          // Value for this node
  private Link<E> n;    // Pointer to next node in list
  private Link<E> p;    // Pointer to previous node

  // Constructors
  Link(E it, Link<E> inp, Link<E> inn) { e = it;  p = inp; n = inn; }
  Link(Link<E> inp, Link<E> inn) { p = inp; n = inn; }

  // Get and set methods for the data members
  public E element() { return e; }                                // Return the value
  public E setElement(E it) { return e = it; }                    // Set element value
  public Link<E> next() { return n; }                             // Return next link
  public Link<E> setNext(Link<E> nextval) { return n = nextval; } // Set next link
  public Link<E> prev() { return p; }                             // Return prev link
  public Link<E> setPrev(Link<E> prevval) { return p = prevval; } // Set prev link
}

Doubly Linked Insert

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Doubly Linked Remove

Settings

Proficient Saving... Error Saving
Server Error
Resubmit