abstract public class DLLNode{ /** * The next branch/node/whatever to process **/ public DLLNode next; /** * The previous branch/node/whatever to process **/ public DLLNode previous; /** * Constructor for DLLNode just sets * next to null **/ public DLLNode(){ next = null; previous = null; } /** * Methods to set and get next elements * @param nextOne next element in list **/ public void setNext(DLLNode nextOne){ this.next = nextOne; } public DLLNode getNext(){ return this.next; } /** * Methods to set and get previous elements * @param prevOne next element in list **/ public void setPrevious(DLLNode prevOne) { this.previous = prevOne; } public DLLNode getPrevious() { return this.previous; } /** Method to remove node from list, fixing links appropriately. * @param node element to remove from list. **/ public void remove(DLLNode node){ DLLNode current = this; while (current.getPrevious() != null) { current = current.getPrevious(); } if (current==node) { System.out.println("I can't remove the first node from the list."); return; }; // While there are more nodes to consider while (current.getNext() != null) { if (current.getNext() == node && current.getNext() != this){ // Set the one after node to be the current if (current.getNext().getNext() != null) { current.getNext().getNext().setPrevious(current); } // And simply make node's next be this next current.setNext(node.getNext()); // Make this node point to nothing node.setNext(null); node.setPrevious(null); return; } current = current.getNext(); } System.out.println("I can't remove this node."); } /** * Insert the input node after this node. * @param node element to insert after this. **/ public void insertAfter(DLLNode node){ // Save what "this" currently points at DLLNode oldNext = this.getNext(); this.setNext(node); node.setPrevious(this); node.setNext(oldNext); oldNext.setPrevious(node); } /** * Return the last element in the list **/ public DLLNode last() { DLLNode current; current = this; while (current.getNext() != null) { current = current.getNext(); }; return current; } /** * **/ public DLLNode first() { DLLNode current; current = this; while (current.getPrevious() != null) { current = current.getPrevious(); } return current; } /** * Return the count of the elements in the list **/ public int count() { DLLNode current = this; int count = 1; while (current.getPrevious() != null) { current = current.getPrevious(); } while (current.getNext() != null) { count++; current = current.getNext(); }; return count; } /** * Add the input node after the last node in this list. * @param node element to insert after this. **/ public void addToTail(DLLNode node){ this.last().insertAfter(node); } /** * Add the input node after the last node in this list. * @param node element to insert after this. **/ public void addToHead(DLLNode node){ this.first().insertAfter(node); } /** * Reverse the list starting at this, * and return the last element of the list. * The last element becomes the FIRST element * of the list, and THIS goes to null. **/ public DLLNode reverse() { DLLNode reversed, temp; // Handle the first node outside the loop reversed = this.last(); reversed.setNext(null); reversed.setPrevious(null); this.remove(reversed); while (this.getNext() != null) { temp = this.last(); this.remove(temp); reversed.addToTail(temp); }; // Now put the head of the old list on the end of // the reversed list. reversed.addToTail(this); // Now put the beginning of the list on the end of // the reversed list while (this.getPrevious() != null) { temp = this.getPrevious(); this.remove(temp); reversed.addToTail(temp); } // At this point, reversed // is the head of the list return reversed; } }