CS111 IntListOps Contract

The IntListOps class contains some commonly used auxiliary class methods that manipulate integer lists. All of these can easily be written in terms of the class methods from the IntList contract; they are provided here for convenience. Also for convenience, the IntListOps class also supplies all of the IntList class methods.

By default, an IntListOps class method is invoked via IntListOps.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 IntListOps IL; // declare but never initialize this variable.

Because IntListOps includes both core IntList methods and auxiliary methods, the above declaration enables using the prefix IL. to access all of these class methods.

Class Methods

public static IntList append (IntList L1, IntList L2)
Returns an integer list whose length is the sum of the lengths of L1 and L2 and whose elements are all the elements of L1, in order, followed by all the elements of L2, in order.

public static boolean equals (IntList L1, IntList L2)
Returns true if L1 and L2 have the same length and the same elements in the same order; otherwise returns false. Note that the equals method behaves differently than == on lists, which determines if two list nodes were created by the same call to prepend. For instance, if L is the list [1,2,3], then L.equals(prepend(1,tail(L))) is true, but L == prepend(1,tail(L)) is false.

public static IntList fromTo (int lo, int hi)
Returns a list of the integers from lo up to hi, inclusive. Returns the empty list if lo is greater than hi.

public static boolean isMember (int n, IntList L)
Returns true if n is an element of list L and false otherwise.

public static int length (IntList L)
Returns the length of L -- i.e., the number of non-empty list nodes in L.

public static IntList postpend (IntList L, int n)
Returns a list whose length is one more than the length of L and whose elements are all the elements of L, in order, followed by n.

public static IntList reverse (IntList L)
Returns a list whose length is the same as that as L and whose elements are all the elements of L in reverse order.

For convenience, the IntListOps class also provides all the class methods of the IntList class:
public static IntList empty()
public static IntList prepend (int n, IntList L)
public static boolean isEmpty (IntList L)
public static int head (IntList L)
public static IntList tail (IntList L)
public static String toString (IntList L)
public static IntList fromString (String s)