//IntList.java - a linked list of integers class IntList { IntListElement head; IntListElement current;//previous; /* Note that there is no constructor. */ /* Instance methods: */ int current(){return current.data;} boolean hasNext(){ if(current != null) return current.next != null; else return head != null; } int next(){ if (current == null) current = head; else current = current.next; return current.data; } void insert(int value){ IntListElement nle = new IntListElement(value); if(current != null){ // current points to a non-empty listmember IntListElement tmp = current.next; current.next = nle; nle.next = tmp; current = nle; } else if(head != null){// one element list current = nle; nle.next = head; head = current; } else {// empty list head = current = nle; } return; } /* We can only (easily) delete the node _after_ * the current node, thus, we should move to a cell * _before_ the head to be able to delete that node * too. */ void moveToHead(){current = null;} public String toString(){ StringBuffer sb = new StringBuffer(); IntListElement cell = head; while(cell != null){ /* Observe the use of the StringBuffer */ sb.append(cell.toString()); sb.append("\n"); cell = cell.next; } return sb.toString(); } }