/** * An IntList is a list of ints. */ public class IntList { private ConsCell start; // first in the list or null /** * Construct a new IntList given its first ConsCell. * @param s the first ConsCell in the list or null */ private IntList(ConsCell s) { start = s; } private static final IntList NIL = new IntList(null); /** * Return an empty IntList. */ public static IntList getEmpty(){ return NIL; } /** * Return an IntList containing the given elements. * @param elements the ints to go in the list */ public static IntList make(int... elements){ IntList list = IntList.getEmpty(); for(int i = elements.length - 1; i >= 0; i--){ list = list.cons(elements[i]); } return list; } /** * Cons the given element h onto us and return the * resulting IntList. * @param h the head int for the new list * @return the IntList with head h and us for a tail */ public IntList cons (int h) { return new IntList(new ConsCell(h,start)); } /** * Get our length. * @return our int length */ public int length() { int len = 0; ConsCell cell = start; while (cell != null) { // while not at end of list len++; cell = cell.getTail(); } return len; } /** * Print ourself to System.out. */ public void print() { System.out.print("["); ConsCell a = start; while (a != null) { System.out.print(a.getHead()); a = a.getTail(); if (a != null) System.out.print(","); } System.out.println("]"); } /** * A ConsCell is an element in a linked list of * ints. */ private static class ConsCell { private int head; // the first item in the list private ConsCell tail; // rest of the list or null /** * Construct a new ConsCell given its head and tail. * @param h the int contents of this cell * @param t the next ConsCell in the list or null */ public ConsCell(int h, ConsCell t) { head = h; tail = t; } /** * Accessor for the head of this ConsCell. * @return the int contents of this cell */ public int getHead() { return head; } /** * Accessor for the tail of this ConsCell. * @return the next ConsCell in the list or null */ public ConsCell getTail() { return tail; } } }