public class DoublyLinkedList implements List { //Data private DNode head,tail; private int num; //Access methods public DNode getHead() { return head; } public void setHead(DNode p) { head = p; } public DNode getTail() { return tail; } public void setTail(DNode p) { tail = p; } public void setNum(int n) { num = n; } public int getNum() { return num; } //Constructor public DoublyLinkedList(){ setHead(new DNode()); setTail(new DNode()); getTail().setLeft(head); getHead().setRight(tail); num = 0; } //Private methods private DNode search(int index) throws IndexOutOfBoundsException { if(index < 0 || index >= size()) throw new IndexOutOfBoundsException(); DNode p = getHead().getRight(); for(int i = 0; i < index; i++) p = p.getRight(); return p; } //List methods public boolean add(Object element) { addLast(element) ; return true; } public void add(int index,Object obj) throws IndexOutOfBoundsException { if(index == 0) addFirst(obj); else if(index == size()) addLast(obj); else { DNode p = search(index); DNode w = new DNode(obj); w.setRight(p); w.setLeft(p.getLeft()); p.getLeft().setRight(w); p.setLeft(w); num++; } } public void addFirst(Object o){ throw new UnsupportedOperationException(); } public void addLast(Object o){ throw new UnsupportedOperationException(); } public boolean contains(Object arg) { throw new UnsupportedOperationException(); } public Object get(int index) { return search(index).getData(); } public Object getFirst() { return get(0); } public Object getLast() { return get(size()-1); } public int indexOf(Object element) { throw new UnsupportedOperationException(); } public int lastIndexOf(Object element) { throw new UnsupportedOperationException(); } public boolean isEmpty() { return num == 0; } public Iterator iterator() { return new DoublyLinkedListIterator(this); } public Object remove(int index) throws IndexOutOfBoundsException { if(index == 0) return removeFirst(); if(index == size()-1) return removeLast(); DNode p = search(index); Object d = p.getData(); p.getRight().setLeft(p.getLeft()); p.getLeft().setRight(p.getRight()); num--; return d; } public Object remove(Object element) { throw new UnsupportedOperationException(); } public Object removeFirst(){ throw new UnsupportedOperationException(); } public Object removeLast(){ throw new UnsupportedOperationException(); } public Iterator reverseIterator() { return new DoublyLinkedListReverseIterator(this); } public Object set(int index,Object obj) { DNode p = search(index); Object result = p.getData(); p.setData(obj); return result; } public int size() { return num; } public String toString() { String result = "["; Iterator it = this.iterator(); while(it.hasNext()) result = result + it.next() + " "; return result + "]"; } }