Week 5. Lists

 

List

List

A list is a collection of elements stored in a certain linear order.

We can refer to the elements in as first, second, third, … The index of an element in is the number of elements before in . The index can be used to specify where to insert a new element into the list, or where to remove an old element from.

Link to original

Array List ADT

Array List (ADT)

An instance is a sequence of of elements. Methods include:

  • size(): return the number of elements in
  • isEmpty(): return true if has no elements, otherwise false
  • get(i): return the element of with index (-st element has index ) Error occurs if or .
  • set(i,e): replace with the element at index and return the old element at this index Error occurs if or .
  • add(i,e): insert a new element into to have index Error occurs if or .
  • remove(i): remove from and return the element at index Error occurs if or .
Link to original

Extending full arrays

Inserting a new element to the list when the array is full: we allocate a new larger array and copy all elements to the new array.

Consider we start with an empty list and perform “add” operations, adding each new element at the end of the list. If we keep increasing the capacity of a full array by , then the running time of this computation is .

If we instead double the capacity, then the running time is for adding the elements when there is room in the array.

Performance

In the array-based implementation of the Array List ADT, if the list has elements:

  • The space used is .
  • Operations size, isEmpty, get, and set run in time.
  • Operation remove(i) runs in time, or, more precisely in .
  • Removing the last element takes time.
  • Adding a new element at the end of the list takes time in the worst case (full array), but only time on average.

Position

Position

The position is a place within a list where a single element is stored. In implementations, positions are nodes of lists.

Link to original

Node List ADT

Node List (ADT)

The Node List ADT models a sequence of positions storing elements of some arbitrary type. It establishes a before / after relation between positions.

It has generic methods:

  • size()
  • isEmpty()

We use an argument throughout to indicate the position. Accessor methods:

  • first()
  • last()
  • prev(p)
  • next(p) Update methods:
  • set(p, e): replace the element at with and return the old eleent
  • remove(p): remove and return the element at
  • addFirst(e) and addLast(e)
  • addBefore(p, e) and addAfter(p, e)
Link to original

Doubly Linked List

A double-linked list provides a natural implementation of the Node List (ADT). Nodes implement Position and store:

  • an element
  • reference to the previous node
  • reference to the next node There are also special trailer and header nodes. (or pointers can be null)

Link to original

Performance

In the implementation of the Node List ADT by means of a doubly linked list:

  • The space used by each node of the list is .
  • The space used by a list with elements is .
  • All the operations of the Node List ADT run in time.
  • The position operation element() runs in time.
  • No efficient implementation of operations which refer to the index of an element of a list.