/** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ /** Linked list implementation */ class LList implements List { private Link head; // Pointer to list header private Link tail; // Pointer to last element protected Link curr; // Access to current element int cnt; // Size of list /** Constructors */ LList(int size) { this(); } // Constructor -- Ignore size LList() { curr = tail = head = new Link(null); // Create header cnt = 0; } /** Remove all elements */ public void clear() { head.setNext(null); // Drop access to links curr = tail = head = new Link(null); // Create header cnt = 0; } /** Insert "it" at current position */ public void insert(E it) { curr.setNext(new Link(it, curr.next())); if (tail == curr) tail = curr.next(); // New tail cnt++; } /** Append "it" to list */ public void append(E it) { tail = tail.setNext(new Link(it, null)); cnt++; } /** Remove and return current element */ public E remove() { if (curr.next() == null) return null; // Nothing to remove E it = curr.next().element(); // Remember value if (tail == curr.next()) tail = curr; // Removed last curr.setNext(curr.next().next()); // Remove from list cnt--; // Decrement count return it; // Return value } /** Set curr at list start */ public void moveToStart() { curr = head; } /** Set curr at list end */ public void moveToEnd() { curr = tail; } /** Move curr one step left; no change if now at front */ public void prev() { if (curr == head) return; // No previous element Link temp = head; // March down list until we find the previous element while (temp.next() != curr) temp = temp.next(); curr = temp; } /** Move curr one step right; no change if now at end */ public void next() { if (curr != tail) curr = curr.next(); } /** @return List length */ public int length() { return cnt; } /** @return The position of the current element */ public int currPos() { Link temp = head; int i; for (i=0; curr != temp; i++) temp = temp.next(); return i; } /** Move down list to "pos" position */ public void moveToPos(int pos) { assert (pos>=0) && (pos<=cnt) : "Position out of range"; curr = head; for(int i=0; i. The vertical * bar represents the current location of the fence. This method * uses toString() on the individual elements. * @return The string representation of this list */ public String toString() { // Save the current position of the list int oldPos = currPos(); int length = length(); StringBuffer out = new StringBuffer((length() + 1) * 4); moveToStart(); out.append("< "); for (int i = 0; i < oldPos; i++) { out.append(getValue()); out.append(" "); next(); } out.append("| "); for (int i = oldPos; i < length; i++) { out.append(getValue()); out.append(" "); next(); } out.append(">"); moveToPos(oldPos); // Reset the fence to its original position return out.toString(); } }