publicclassSLList{ publicclassIntNode{ publicint item; public IntNode next;
publicIntNode(int i, IntNode n){ item = i; next = n; } } private IntNode first;
publicSLList(int x){ first = new IntNode(x,null); } /** Adds x to the front of the list */ publicvoidaddFirst(int x){ first = new IntNode(x, first); }
/** returns the first item in the list */ publicintgetFirst(){ return first.item; }
/** Adds x to the last of the list*/ publicvoidaddLast(int x){ IntNode p = first; while(p.next!=null){ p = p.next; } IntNode q = new IntNode(x,null); p.next = q; }
/** Returns the size of the list that starts at IntNode p*/ privatestaticintsize(IntNode p){ if(p.next ==null){ return1; }else{ return1+size(p.next); } }
/** Returns the size of a given instance of the SLList */ publicintsize(){ return size(first); } publicstaticvoidmain(String[] args){ SLList L = new SLList(15); L.addFirst(10); L.addFirst(5); L.addLast(20); System.out.println(L.size());