hwtest.mlist

package hwtest.mlist

Type members

Classlikes

final class MHeader[A](var info: A, var front: MList)

A "header" node for MLists.

A "header" node for MLists.

The info field stores information about the list, such as its length.

The front field stores a pointer to the front of the list. Maintaining this pointer in a header node helps avoid a very common bug where the front of the list changes (either a new list node was added or the front node was removed) but a pointer elsewhere in the code still points to the old front. With header nodes, only pointers to the header should be shared, and every operation that changes the front of the list should update the header.

Companion:
object
object MHeader
Companion:
class
sealed abstract class MList

A class of singly-linked mutable lists of integers.

A class of singly-linked mutable lists of integers.

MList is very similar to hwtest.slist.SList, but with the following differences:

  1. head_= and tail_= method supporting assignment to the head and tail of a node.
  2. == and hashing based on pointers rather than the contents of a node.
  3. numerous complications related to the possibility that an MList could contain a cycle.

Here's how the MLists handle cycles:

  1. A cyclic list will be displayed as (for example)
      MList(1, 2, *3, 4->*)

which indicates that the tail field of the 4 node points back to the 3 node. 2. Two cyclic lists are considered equivalent only if they have EXACTLY the same shape. For example, the following lists are arguably equivalent (because they all consist of a 1 followed by an infinite number of 2s) but are NOT considered equivalent by this package:

      MList(1, *2->*)
      MList(1, 2, *2->*)
      MList(1, *2, 2->*)
  1. To write tests involving cyclic lists, use the format
      (1 *2 3)
where the `*` indicates the beginning of the cycle.
The `*` must be followed by at least one element.
Whitespace is allowed between the `*` and the following
element.
Companion:
object
object MList
Companion:
class