//---------------------------------------------------------------------------- // RefUnsortedList.java by Dale/Joyce/Weems Chapter 6 // // Implements the ListInterface using references (a linked list). // // Null elements are not permitted on a list. // // ajg version: implements UNBOUNDEDListInterface, format, while-->for // initialize in declarations so default noarg constructor OK //---------------------------------------------------------------------------- package ch06.lists; import support.LLNode; public class RefUnsortedList implements UnboundedListInterface { protected int numElements = 0; // number of elements in this list protected LLNode currentPos=null; // current position for iteration protected LLNode list=null; // first node (null for empty list) // set by find method protected LLNode location; // node containing element, if found protected LLNode previous; // node preceeding location or null public void add(T element) { // add and element (at the beginning) LLNode newNode = new LLNode(element); newNode.setLink(list); list = newNode; numElements++; } // Search for an element e such that e.equals(target). Return outcome. // If successful, set location to a node containing e, and previous // to the preceeding node (undefined if location==list, i.e., first node) protected boolean find(T target) { for (location=list; location!=null; location=location.getLink()) if (location.getInfo().equals(target)) // if they match return true; else previous = location; return false; } public int size() { return numElements; } // Does list contains an element e such that e.equals(element) public boolean contains (T element) { return find(element); } // Removes an element e such that e.equals(element). Returns outcome public boolean remove (T element) { boolean found = find(element); if (found) { if (location == list) // remove first node list = list.getLink(); else // remove non-first node previous.setLink(location.getLink()); numElements--; } return found; } // Return an element e such that e.equals(element) or null if no such e public T get(T element) { return (find(element)) ? location.getInfo() : null; } // Returns a nicely formatted string that represents this list. public String toString() { String listString = "List:\n"; for (LLNode currNode=list; currNode!=null; currNode=currNode.getLink()) listString = listString + " " + currNode.getInfo() + "\n"; return listString; } // Initialize current position for an iteration through this list. public void reset() { currentPos = list; } // Return the element at the current position and advance to next position. // If (initially) at the last element, advance to the first element // // Preconditions: The list is not empty // The list has been reset // The list has not been modified since the most recent reset public T getNext() { T next = currentPos.getInfo(); if (currentPos.getLink() == null) currentPos = list; else currentPos = currentPos.getLink(); return next; } } // Local Variables: // compile-command: "cd ../..; javac ch06/lists/RefUnsortedList.java" // End: