// Implementation of lists, using doubly linked elements and keeping track of current element. // (c) 1996 duane a. bailey, modified 2/28/98 by Kim Bruce, modified 10/3/99 Andrea Danyluk import structure.*; public class CurDoublyLinkedList extends DoublyLinkedList { protected DoublyLinkedListElement current; // special current elt protected boolean offRight, // are we off list to right or left? offLeft; public CurDoublyLinkedList() // post: constructs an empty list { super(); current = null; offRight = true; offLeft = true; } public void first() { // pre: list is non-empty // post: current set to first element of list } public void last() { // pre: list is non-empty // post: current set to last element of list } public void next() { // pre: list is non-empty && not isOffRight() // post: if is off left then make head the current elt, if current is tail then set // current to null and make offRight true, else set current elt to be next // element of list. // set offRight and offLeft appropriately in all cases } public void back() { // pre: list is non-empty && not isOffLeft() // post: if is off right then make tail the current elt, if current is head then set // current to null and make offLeft true, else set current elt to be previous // element of list. // set offRight and offLeft appropriately in all cases } public boolean isOffRight() { // pre: list is non-empty // post: return whether last move caused to fall off right end of list } public boolean isOffLeft() { // pre: list is non-empty // post: return whether last move caused to fall off left end of list } public Object getCurrent() { // pre: List is not empty & !(isOffRight() || isOffLeft()) // post: Return value in current node. } public void addAfterCurrent(Object value) { // pre: value is not null, List non-empty & !(isOffRight() || isOffLeft()) // post: adds element after current. New elt is current. } public void deleteCurrent() { // pre: list is non-empty & !(isOffRight() || isOffLeft()) // post: Current element is deleted, successor is new current elt // If deleted tail, then current is null and offRight is true } public void addToHead(Object value) // pre: value is not null // post: adds element to head of list and make it current { // construct a new element, making it the head super.addToHead(value); current = head; offRight = false; offLeft = false; } public Object removeFromHead() { // pre: list is not empty // post: removes first value from list, successor is current } public void addToTail(Object value) { // pre: value is not null // post: adds new value to tail of list and make it current } public Object removeFromTail() { // pre: list is not empty // post: removes value from tail of list, offRight is true // and current is null } public boolean contains(Object value) { // pre: value not null // post: returns true iff value is in the list // sets current to first node containing value if there is one, // else set current to null and make offRight true } public void clear() { // post: removes all the elements from the list } public static void main(String[] args) { System.out.println("Don't forget to do thorough testing..."); } }