O - 1
Lists, Too
O - 2
Integer Linked Lists
An integer list is either: (1) empty, represented by Ø (null)
- r
(2) a listNode, whose head is an integer and whose tail is an integer list
17
- 8
4
Ø L
O - 3
Class Methods of IntList Contract
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 is n and whose tail is L.
public static boolean isEmpty (IntList L)
Returns true if L is an empty integer list and false if L is an integer list node.
public static int head (IntList L)
Returns the integer that is the head component of the integer list node L.
public static IntList tail (IntList L)
Returns the integer list that is the tail component of the integer list node L.
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 .
public static String toString (IntList L)
Returns a string representation of of the integer list L in which the integer list elements are separated by commas and delimited by square brackets. For example, if L1 is a list containing the sequence of integers 6, -17 and 42, then toString(L1) returns "[6,-17,42]" .
public static IntList fromString (String s)
If s is the printed representation of an integer list (i.e., comma separated integers delimited by square brackets), returns an IntList with that representation. For example, fromString("[7,-2,4]") returns an 3-element integer list with elements 7,-2 , and 4. O - 4
// Returns true if all the elements in L are positive, false otherwise public static boolean areAllPositive(IntList L) { }
Recursive List Method
O - 5
// Returns true if at least one element in L is positive, false otherwise public static boolean isSomePositive(IntList L) { }
Recursive List Method
O - 6
Filter Methods
More formally: A filter method transforms one list to another by keeping only those elements satisfying a predicate
L1 L2
filter