// 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 // A modified version of the linked list implementation // that uses a freelist. Only a few methods are actually different. class LList implements List { // Linked list class (freelist) private Link head; // Pointer to list header private Link tail; // Pointer to last Object in list private Link curr; // Position of current Object LList(int sz) { setup(); } // Constructor -- Ignore sz LList() { setup(); } // Constructor private void setup() // Do the initializations { tail = head = curr = Link.get(null, null); } // Create header node public void clear() { // Remove all Elems from list head.setNext(null); curr = tail = head; // Reinitialize } // The following three methods, assert, append, and remove, are the // only ones affected by the use of the freelist methods. // Insert Object at current position public void insert(Object it) { Assert.notNull(curr, "No current element"); curr.setNext(Link.get(it, curr.next())); // Get Link if (tail == curr) // Appended new Object tail = curr.next(); } public void append(Object it) { // Insert Object at tail of list tail.setNext(Link.get(it, null)); // Get Link tail = tail.next(); } public Object remove() { // Remove and return current Elem Assert.notFalse(isInList(), "No current element"); Object it = curr.next().element(); // Remember value if (tail == curr.next()) tail = curr; // Removed last: set tail Link tempptr = curr.next(); curr.setNext(curr.next().next()); // Remove from list tempptr.release(); // Release Link 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 Link temp = head; if ((curr == null) || (curr == head)) // No previous Object { curr = null; return; } // so just return while ((temp != null) && (temp.next() != curr)) temp = temp.next(); curr = temp; } 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