The IntList class describes immutable linked lists of integers.
An integer list is defined recursively as either
Integer lists are immutable in the sense that the head or tail of a list node
cannot be changed after the list node has been created (via the prepend() method).
There are no public constructor or instance methods for integer
lists; they are created and manipulated by the class methods below.
By default, an IntList class method is invoked via
IntList.methodName(args), but this can
be abbreviated to IL.methodName(args)
by including the following "magical" declaration in the body
of a class:
public static IntList IL; // declare but never initialize this variable.
Class Methods
public static IntList empty()
Returns an empty integer list.
public static IntList prepend (int n, IntList L)
Returns a new integer list node whose head isnand whose tail isL. From another perspective, returns a list whose length is one more than the length ofLand whose elements arenfollowed by the elements ofL, in order.
public static boolean isEmpty (IntList L)
ReturnstrueifLis an empty integer list andfalseifLis an integer list node.
public static int head (IntList L)
Returns the integer that is the head component of the integer list nodeL. Signals an exception ifLis empty.
public static IntList tail (IntList L)
Returns the integer list that is the tail component of the integer list nodeL. Signals an exception ifLis empty.
public static String toString (IntList L)
Returns a string representation of the integer listLin which the integer list elements are separated by commas and delimited by square brackets. For example, ifL1is a list containing the sequence of integers 6, -17 and 42, thentoString(L1)returns"[6,-17,42]".
public static IntList fromString (String s)
Ifsis the printed representation of an integer list (i.e., comma separated integers delimited by square brackets), returns anIntListwith that representation. For example,fromString("[7,-2,4]")returns an 3-element integer list with elements7,-2, and4. Ifsis not the printed representation of an integer list, signals an error.