/** Removes all of the elements from this list. */ public void clear() { if (size != 0) { first.next = null; first = null; last = null; size = 0; } }
public E getFromEnd() { if (size == 0) return null; E result = end.nd; end = end.prev; if (end != null) end.next = null; else begin = null; size--; return result; }
/** Inserts the given element at the end of this list. */ public void addLast(T element) { if (size == 0) { last = new ListElement<T>(element); first = last; } else { ListElement<T> templast = new ListElement<T>(element); last.next = templast; last = templast; } size++; }
private void swap(ListElement A, ListElement B, DoublyLinkedList in) { // next-zeiger. ListElement ntemp = A.next; A.next = B.next; B.next = ntemp; A.next.prev = A; B.next.prev = B; // prev-zeiger. ListElement ptemp = A.prev; A.prev = B.prev; B.prev = ptemp; A.prev.next = A; B.prev.next = B; if (A == in.first) { in.first = B; } else if (B == in.first) { in.first = A; } }
/** Inserts the given element at the beginning of this list. */ public void addFirst(T element) { // If there are no elements the first and last has the same value if (size == 0) { first = new ListElement<T>(element); last = first; } else { ListElement<T> second = first; first = new ListElement<T>(element); first.next = second; } size++; }