package test; import java.util.*; public class DoublyLinkedList { class Node { //Node Data private E value; private Node prev; private Node next; //Node constructor Node(E value) { this.value = value; } Node(E value, Node prev, Node next) { this.value = value; setPrev(prev); setNext(next); } //Node Setter void setPrev(Node prev) { this.prev = prev; } void setNext(Node next) { this.next = next; } //Node Getter Node getPrev() { return prev; } Node getNext() { return next; } E getValue() { return value; } //<>= private Node head = new Node(null); private Node tail = new Node(null); private int length = 0; //<>= { head.setPrev(null); head.setNext(tail); tail.setPrev(head); tail.setNext(null); } // <>= public Node get(int index) throws IndexOutOfBoundsException { if (index < 0 || index > length) { throw new IndexOutOfBoundsException(); } else { Node cursor = head; for (int i = 0; i < index; i++) { cursor = cursor.getNext(); } return cursor; } } //<>= public E remove(int index) throws IndexOutOfBoundsException { if (index == 0) { throw new IndexOutOfBoundsException(); } else { Node result = get(index); result.getNext().setPrev(result.getPrev()); result.getPrev().setNext(result.getNext()); length--; return result.getValue(); } } //<>= public void add(int index, E value) throws IndexOutOfBoundsException { Node cursor = get(index); Node temp = new Node(value); temp.setPrev(cursor); temp.setNext(cursor.getNext()); cursor.getNext().setPrev(temp); cursor.setNext(temp); length++; } public void addHead(E value) { Node cursor = head; Node temp = new Node(value); temp.setPrev(cursor); temp.setNext(cursor.getNext()); cursor.getNext().setPrev(temp); cursor.setNext(temp); length++; } public void addTail(E value) { Node cursor = tail.getPrev(); Node temp = new Node(value); temp.setPrev(cursor); temp.setNext(cursor.getNext()); cursor.getNext().setPrev(temp); cursor.setNext(temp); length++; } //<>= public int size() { return length; } public boolean isEmpty() { return length == 0; } public String toString() { StringBuffer result = new StringBuffer(); result.append("(head) - "); Node temp = head; while (temp.getNext() != tail) { temp = temp.getNext(); result.append(temp.getValue() + " - "); } result.append("(tail)"); return result.toString(); } } //<>= public static void main(String argv[]) { DoublyLinkedList list = new DoublyLinkedList(); //Error here-> the type DoublyLinkedList is not generic it cannot be parameterized with arguments list.addHead(new Integer(1)); list.addHead(new Integer(2)); list.addTail(new Integer(9)); list.addHead(new Integer(3)); list.addTail(new Integer(11)); list.add(2, new Integer(0)); System.out.println(list); list.remove(list.size()); System.out.println(list); } }