public class DoublyLinkedList { private Node head; private Node tail; private int count; public void addHead(Object x){ if(count==0) head = tail=new Node(x); else { Node current = new Node(x); current.next = head; head=current; } count++; } public void addTail(Object x){ if(count==0) addHead(x); else{ Node current = new Node(x); tail.next = current; tail=current; } count++; } public Node getHead() { if(count==0) return null; else return head; } public Node getTail() { if(count==0) return null; else return tail; } public void printList(){ if(count==0) System.out.println("the list is empty"); else{ Node current; current=head; while(current!=null){ System.out.println(current.data); current=current.next; } } } public void insertAtPos(int at, Object data) { if (at == 0) { insertAtHead(data); return; } else if (at > length() ) { System.out.println("Error position out of range"); return; } else if (at == length() ) { insertAtTail(data); return; } else { int points = 0; Node current = head; while(current != null && points < at){ current = current.getNext(); points++; } Node node = new Node(data); node.setPrev(current); node.setNext(current.getNext()); current.setNext(node); current.getNext().setPrev(node); } } public void insertAtHead(Object data) { Node node = new Node(data); if (head == null) { head = node; tail = node; return; } else { node.setNext(head); head.setPrev(node); head = node; } } public void insertAtTail(Object data) { Node node = new Node(data); if (head == null) { head = node; tail = node; return; } else { tail.setNext(node); node.setPrev(tail); tail = node; } } public int length() { Node current = head; int size = 0; while (current != null) { size++; current = current.getNext(); } return size; } public boolean isEmpty() { if(count == 0) return true; else return false; } public String toString() { String result = "head -->"; Node current = head; while (current != null) { if (current.getNext() == null) result += current + " -->"; else result += current + " <--> "; current = current.getNext(); } return result + " null"; } public void deleteHead(){ if(count==0) System.out.println("empty"); else{ head=head.next; count--; } } public void deleteTail(){ if(count==0) System.out.println("the list is empty"); else{ Node current=head; while(current.next!=tail){ current=current.next; } current.next=null; tail=current; count--; } } public void deleteP(int index){ if(index==0) deleteHead(); else if(index==count) deleteTail(); else{ Node current=head; for(int i=0; i