import java.util.Iterator; public class DoublyLinkedList { private DoubleListNode head, tail; public DoublyLinkedList(Type element) { head = tail = new DoubleListNode(element); } public DoubleListNode getFirst() { return head; } public DoubleListNode getLast() { return tail; } public static void main(String[] args) { // Using the most basic implementation. DoublyLinkedList list = new DoublyLinkedList("A"); DoubleListNode node = list.getFirst(); node.setNext(new DoubleListNode("B")); node.getNext().setNext(new DoubleListNode("C")); node.getNext().getNext().setNext(new DoubleListNode("D")); while (node != null) { System.out.println(node); node = node.getNext(); } // Using add methods and iterator. list = new DoublyLinkedList("1"); list.add("2", 1); list.add("3", 2); list.add("4", 3); list.remove(2); Iterator iter = list.iterator(false); while (iter.hasNext()) { System.out.println(iter.next()); } // Add 3 again, reverse list, and print out. list.add("3", 2); list.reverse(); iter = list.iterator(true); System.out.println("Reversed list:"); while (iter.hasNext()) { System.out.println(iter.next()); } } // Extra methods // Insert after current element. public void addToFront(Type element) { DoubleListNode newHead = new DoubleListNode(element); newHead.setNext(head); head.setPrevious(newHead); head = newHead; } public void addToBack(Type element) { DoubleListNode newTail = new DoubleListNode(element); newTail.setPrevious(tail); tail.setNext(newTail); tail = newTail; } public void add(Type element, int index) { if (index == 0) { DoubleListNode newHead = new DoubleListNode(element); newHead.setNext(head); head = newHead; } else { int count = 1; DoubleListNode next = head; while (count < index && next.getNext() != null) { next = next.getNext(); count++; } DoubleListNode newNode = new DoubleListNode(element); newNode.setNext(next.getNext()); newNode.setPrevious(next); next.setNext(newNode); } } public Type remove(int index) { int count = 0; DoubleListNode current = head; while (count < index && current != null) { current = current.getNext(); count++; } if (current == null) { return null; } else { Type element = current.getElement(); if (current.getPrevious() != null) { current.getPrevious().setNext(current.getNext()); } if (current.getNext() != null) { current.getNext().setPrevious(current.getPrevious()); } return element; } } public void reverse() { DoubleListNode current = head, next = head.getNext(); current.setNext(null); while (next != null) { DoubleListNode temp = next; next = next.getNext(); temp.setNext(current); current.setPrevious(temp); current = temp; } current.setPrevious(null); tail = head; head = current; } public Iterator iterator(boolean backwards) { if (backwards) { return new SLLIterator(tail, true); } else { return new SLLIterator(head, false); } } class SLLIterator implements Iterator { private DoubleListNode current; private boolean backwards; public SLLIterator(DoubleListNode head, boolean backwards) { current = head; this.backwards = backwards; } public boolean hasNext() { return current != null; } public IteratorType next() { if (current == null) { return null; } else { IteratorType next = current.getElement(); if (backwards) { current = current.getPrevious(); } else { current = current.getNext(); } return next; } } public void remove() { } } } class DoubleListNode { private NodeType element; private DoubleListNode next; private DoubleListNode previous; public DoubleListNode(NodeType element) { this.element = element; } public void setNext(DoubleListNode next) { this.next = next; } public void setPrevious(DoubleListNode previous) { this.previous = previous; } public DoubleListNode getNext() { return next; } public DoubleListNode getPrevious() { return previous; } public NodeType getElement() { return element; } public String toString() { return element.toString(); } }