public class LinkedList> implements LinkedListInterface { private class Node { public E data; public Node next; public Node(E data, Node next) { this.data = data; this.next = next; } } private Node head = null; private Node tail = null; private int size = 0; @Override public int size() { return size; } @Override public boolean isEmpty() { return size==0; } @Override public E first() { if (isEmpty()) { return null; } else { return (E)head.data; } } @Override public E last() { if (isEmpty()) { return null; } else { return (E)tail.data; } } @Override public void addLast(E e) { Node newest = new Node(e, null); if (isEmpty()) { head = newest; } else { tail.next=newest; } tail = newest; size++; } @Override public void addFirst(E e) { Node newest = new Node(e, null); if (isEmpty()) { tail=newest; } else { newest.next=head; } head=newest; size++; } @Override public E removeFirst() { if (isEmpty()) {return null;} E target = (E)head.data; head = head.next; size--; if (isEmpty()) {tail = null;} return target; } @Override public E removeLast() { if (isEmpty()) return null; if (head==tail) { E target=(E)head.data; head=null; tail=null; size=0; return target; } Node prev = null; for (Node n=head; n!=tail; n=n.next) { prev=n; } prev.next=null; E target = (E)tail.data; tail=prev; size--; return target; } @Override public E remove(E e) { if (isEmpty()) {return null;} Node prev=null; Node current = head; while (current !=null && current.data != e) { prev=current; current = current.next; } if (current==null) { return null; } if (current==tail) { tail=prev; } if (current==head) { return removeFirst(); } prev.next=current.next; size--; if (size==0) { head=null; tail=null; } return e; } @Override public E find(E e) { Node curr = head; while (curr!=null) { if (curr.data.compareTo(e)==0) { return (E)curr.data; } curr=curr.next; } return null; } public String toString() { StringBuffer s = new StringBuffer(); for (Node n=head; n!=null; n=n.next) { s.append( n.data.toString()); if (n != tail) { s.append("\n"); } } return s.toString(); } }