//************************************************* // // An example of linked lists, initially for lists of // integers. We define the class and appropriate methods // //************************************************** public class IntList { private IntNode head; public IntList(){ head = null; } // // An IntList is a container that has a pointer to the // first node in the list. The pointer is instance variable // head. The IntList is made up of linked IntNode's // public boolean isEmpty(){return head == null;} // // is the IntList empty? // public String toString(){ if (isEmpty()) return "()"; else return "(" + head + ")"; } // // So that you can print an IntList // i.e. IntList X = new IntList(); // X.add(33); // System.out.println(X); // public void add(int data){head = new IntNode(data,head);} // // add a new IntNode to the front of the list // public void delete(int e){head = delete(head,e);} // // delete ALL IntNode's in the IntList that have // data == e! // We make the deletion using the private method below, // which operates on the IntNode's directly // private IntNode delete(IntNode x, int e) { if (x == null) // (1) return null; // (2) else if (x.getData() == e) // (3) return delete(x.getNext(),e); // (4) else { // (5) x.setNext(delete(x.getNext(),e)); // (6) return x; // (7) } } // // (1) & (2) deletion from an empty IntNode delivers null // (3) & (4) the data in IntNode x equals e, so return // a value via the recursuve call to delete // on the next IntNode linked to x! That is // SKIP THIS IntNode! // (5) & (6) & (7) the data in the current IntNode x // is NOT equal to e, so we are going to return // x as a result BUT we will have its next attribute // contain the following IntNode's with e deleted // from it. // NOTE: we have two recursive calls (at (4) and (6)), and these // correspond to deleting e from "the rest of the list" // public int size(){return -999;} // // IMPLEMENT ME! // // return the number of elements in the list // zero if empty // public int sum(){return -9999;} // // IMPLEMENT ME! // // return the sum of the data in the list // zero if empty // public int max() throws IntListException {return -9999;} // // IMPLEMENT ME! // // return the largest data in the list. // throw a IntListException if empty // public boolean isPresent(int e) {return false;} // // IMPLEMENT ME! // // return true if any IntNode in the IntList has // data == e. // return false otherwise // private void show(){ System.out.println(this + " isEmpty()=" + isEmpty() + " size()=" + size() + " sum()=" + sum() + " max()=" + max()); } public static void main(String[] args) throws Exception { // incase you want to see how it goes java IntList IntList L1 = new IntList(); System.out.println(L1.isEmpty() + " " + L1); L1.add(2); System.out.println(L1.isEmpty() + " " + L1); L1.add(1); System.out.println(L1.isEmpty() + " " + L1); IntList L2 = new IntList(); for (int i=10;i>0;i--) L2.add(i); System.out.println(L2.isEmpty() + " " + L2); for (int i=10;i>0;i--) L2.add(i); L2.delete(50); System.out.println(L2.isEmpty() + " " + L2); L1.delete(1); System.out.println(L1.isEmpty() + " " + L1); L1.delete(2); System.out.println(L1.isEmpty() + " " + L1); L2.show(); L1.show(); System.out.println(L2.max()); } }