// // class to represented ordered linked lists of integers (i.e., // lists of integers in ascending order) // public class OrderedIntList { ListElem first = null ; // no constructor method needed // returns true iff list is empty public boolean isEmpty() { return (first == null) ; } // if e is in the list, does nothing. // if e is not in the list, adds it, maintaining ascending // order. public void addElement(int e) { if (isEmpty()) { first = new ListElem(e, null) ; } else { first = first.addElement(e) ; } } // if e is not in the list, does nothing. // if e is in the list, removes it. public void removeElement(int e) { if (!isEmpty()) { first = first.removeElement(e) ; } } // returns true iff e is in the list. public boolean hasElement(int e) { boolean returnVal ; if (isEmpty()) { returnVal = false ; } else { returnVal = first.hasElement(e) ; } return returnVal ; } // returns a string representing the list (values separated // by spaces) public String toString() { String returnVal ; if (isEmpty()) { returnVal = "{ }" ; } else { returnVal = "{ " + first + " }" ; } return returnVal ; } // nested class for list elements -- // static because we don't need/want "enclosing objects" // for list elements static class ListElem { int elem ; ListElem next ; // constructor method ListElem(int e, ListElem n) { elem = e ; next = n ; } // if e is in L, returns head of L. // if e is not in L, returns head of a list // consisting of L together with e, in // ascending order. // where L = list starting with this link. ListElem addElement(int e) { ListElem returnVal ; if (e < elem) { returnVal = new ListElem(e, this) ; } else if (e == elem) { returnVal = this ; } else if (next == null) { returnVal = this ; next = new ListElem(e, null) ; } else { // e > elem & next != null returnVal = this ; next = next.addElement(e) ; } return returnVal ; } // if e is not in L, returns head of L. // if e is in L, returns head of a list // consisting of L minus e. // where L = list starting with this link. ListElem removeElement(int e) { ListElem returnVal ; if (e < elem) { returnVal = this ; } else if (e == elem) { returnVal = next ; } else if (next == null) { returnVal = this ; } else { // e > elem & next != null returnVal = this ; next = next.removeElement(e) ; } return returnVal ; } // returns true iff e is in the list starting // with this link. boolean hasElement(int e) { boolean returnVal ; if (e < elem) { returnVal = false ; } else if (e == elem) { returnVal = true ; } else if (next == null) { returnVal = false ; } else { // e > elem & next != null returnVal = next.hasElement(e) ; } return returnVal ; } // returns a string representing the list (values // separated by spaces) public String toString() { String returnVal ; if (next == null) { returnVal = "" + elem ; } else { returnVal = elem + " " + next.toString() ; } return returnVal ; } } // end of inner class // main method to test other methods. public static void main(String[] args) { OrderedIntList l1 = new OrderedIntList() ; System.out.println("New list: " + l1) ; checkcontains(l1, 0) ; checkadd(l1, 4) ; checkadd(l1, 2) ; checkadd(l1, 8 ) ; checkadd(l1, 6) ; checkadd(l1, 4) ; checkadd(l1, 2) ; checkadd(l1, 8) ; checkadd(l1, 6) ; checkcontains(l1, 0) ; checkcontains(l1, 2) ; checkcontains(l1, 4) ; checkcontains(l1, 5) ; checkcontains(l1, 6) ; checkcontains(l1, 8) ; checkcontains(l1, 10) ; checkremove(l1, 0) ; checkremove(l1, 5) ; checkremove(l1, 10) ; checkremove(l1, 2) ; checkremove(l1, 6) ; checkremove(l1, 8) ; checkremove(l1, 4) ; checkremove(l1, 0) ; } static void checkcontains(OrderedIntList l, int e) { System.out.println("List contains " + e + "?: " + l.hasElement(e)) ; } static void checkadd(OrderedIntList l, int e) { l.addElement(e) ; System.out.println("After adding " + e + ": " + l) ; } static void checkremove(OrderedIntList l, int e) { l.removeElement(e) ; System.out.println("After removing " + e + ": " + l) ; } }