// Source code example for "A Practical Introduction to Data Structures // and Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer interface List { // List ADT public void clear(); // Remove all Objects from list public void insert(Object item); // Insert Object at curr position public void append(Object item); // Insert Object at tail of list public Object remove(); // Remove/return current Object public void setFirst(); // Set current to first position public void next(); // Move current to next position public void prev(); // Move current to prev position public int length(); // Return current length of list public void setPos(int pos); // Set current to specified pos public void setValue(Object val); // Set current Object's value public Object currValue(); // Return value of current Object public boolean isEmpty(); // Return true if list is empty public boolean isInList(); // True if current is within list public void print(); // Print all of list's elements } // interface List class Link { // A singly linked list node private Object element; // Object for this node private Link next; // Pointer to next node in list Link(Object it, Link nextval) // Constructor 1 { element = it; next = nextval; } // Given Object Link(Link nextval) { next = nextval; } // Constructor 2 Link next() { return next; } Link setNext(Link nextval) { return next = nextval; } Object element() { return element; } Object setElement(Object it) { return element = it; } } // class Link // Elem interface. This is just an Object with // support for a key field. interface Elem { // Interface for generic element type public abstract int key(); // Key used for search and ordering } // interface Elem // Sample implementation for Elem interface. // A record with just an int field. public class IElem implements Elem { private int value; public IElem(int v) { value = v; } public IElem() {value = 0;} public int key() { return value; } public void setkey(int v) { value = v; } // Override Object.toString public String toString() { return Integer.toString(value); } } class LList implements List { // Linked list class private Link head; // Pointer to list header private Link tail; // Pointer to last Object in list protected Link curr; // Pointer to current Object LList(int sz) { setup(); } // Constructor -- Ignore sz LList() { setup(); } // Constructor private void setup() // Do initialization { tail = head = curr = new Link(null); } // Create header node public void clear() { // Remove all Objects from list head.setNext(null); // Drop access to rest of links curr = tail = head; // Reinitialize } // Insert Object at current position public void insert(Object it) { Assert.notNull(curr, "No current element"); curr.setNext(new Link(it, curr.next())); if (tail == curr) // Appended new Object tail = curr.next(); } // Insert Object at end of list public void append(Object it) { tail.setNext(new Link(it, null)); tail = tail.next(); } public Object remove() { // Remove and return current Object if (!isInList()) return null; Object it = curr.next().element(); // Remember value if (tail == curr.next()) tail = curr; // Removed last: set tail curr.setNext(curr.next().next()); // Remove from list return it; // Return value removed } public void setFirst() // Set curr to first position { curr = head; } public void next() // Move curr to next position { if (curr != null) curr = curr.next(); } public void prev() { // Move curr to previous position if ((curr == null) || (curr == head)) // No previous Object { curr = null; return; } // so just return Link temp = head; // Start at front of list while ((temp != null) && (temp.next() != curr)) temp = temp.next(); curr = temp; // Found previous link } public int length() { // Return current length of list int cnt = 0; for (Link temp = head.next(); temp != null; temp = temp.next()) cnt++; // Count the number of Objects return cnt; } public void setPos(int pos) { // Set curr to specified position curr = head; for(int i=0; (curr!=null) && (i