/** * add element * * @param element * @return */ @Override publicbooleanadd(T element) { return addLast(element); }
/** * add element at index * * @param index * @param element * @return */ @Override publicbooleanadd(int index, T element) { if (index < 0 || index > size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } if (index == size) { return add(element); } else { return addBefore(index, element); } }
/** * Add Last element * * @param element * @return */ privatebooleanaddLast(T element) { Node<T> last = tail; Node<T> newNode = newNode<>(null, element); tail = newNode; // if linked list is empty if (last == null) { head = newNode; } else { last.next = newNode; } size++; returntrue; }
/** * add element before certain element * * @param index * @param element * @return */ privatebooleanaddBefore(int index, T element) { checkPositionIndex(index); // prev node Node<T> prev = null; Node<T> x = head; for (inti=0; i < index; i++) { prev = x; x = x.next; } // current node Node<T> current = x; // new node Node<T> newNode = newNode<>(current, element); // if current node is head if (prev == null) { head = newNode; } else { prev.next = newNode; } size++; returntrue; }
/** * remove element * * @param element * @return */ @Override publicbooleanremove(T element) { Node<T> prev = null; Node<T> x = head; if (element == null) { while (x != null && x.element != null) { prev = x; x = x.next; } } else { while (x != null && !x.element.equals(element)) { prev = x; x = x.next; } }
// if this linked is null OR don't find element if (x == null) { returnfalse; }
Node<T> next = x.next;
// if delete node is head if (prev == null) { head = next; } else { prev.next = next; } // if delete node is tail if (next == null) { tail = prev; }
// for GC x.element = null; x = null;
size--; returntrue; }
/** * remove element by index * * @param index * @return */ @Override public T remove(int index) { checkPositionIndex(index); Node<T> prev = null; Node<T> x = head; for (inti=0; i < index; i++) { prev = x; x = x.next; }
// if linked is empty if (x == null) { returnnull; }
Node<T> next = x.next;
// if delete node is head if (prev == null) { head = next; } else { prev.next = next; }
// if delete node is tail if (next == null) { tail = prev; } size--; return x.element; }
/** * set element by index * * @param index * @param element * @return old element */ @Override public T set(int index, T element) { checkPositionIndex(index); Node<T> node = node(index); ToldElement= node.element; node.element = element; return oldElement; } /** * get element by index * * @param index * @return */ @Override public T get(int index) { Node<T> node = node(index); returnnode== null ? null : node.element; }
/** * get element by index * * @param index * @return */ private Node<T> node(int index) { checkPositionIndex(index); Node<T> x = head; for (inti=0; i < index; i++) { x = x.next; } return x; }
/** * check index * * @param index */ privatevoidcheckPositionIndex(int index) { if (index < 0 || index >= size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } }
/** * clear list */ @Override publicvoidclear() { for (Node<T> x = head; x != null; ) { Node<T> next = x.next; x.element = null; x.next = null; x = next; } head = tail = null; size = 0; }
/** * contain certain element * * @param element */ @Override publicbooleancontains(T element) { if (element == null) { for (Node<T> x = head; x != null; x = x.next) { if (x.element == null) { returntrue; } } } else { for (Node<T> x = head; x != null; x = x.next) { if (x.element.equals(element)) { returntrue; } } } returnfalse; }
/** * get list size * * @return */ @Override publicintsize() { return size; }
/** * Linked List Node * * @param <T> */ privateclassNode<T> { private Node<T> next; private T element;
/** * size */ privateintsize=0; /** * head element */ private Node<T> head = null; /** * tail element */ private Node<T> tail = null;
/** * add element * * @param element * @return */ @Override publicbooleanadd(T element) { return addLast(element); }
/** * add element at index * * @param index * @param element * @return */ @Override publicbooleanadd(int index, T element) { if (index < 0 || index > size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } if (index == size) { return add(element); } else { return addBefore(element, node(index)); } }
/** * Add Last element * * @param element * @return */ privatebooleanaddLast(T element) { final Node<T> last = tail; Node<T> newNode = newNode<>(last, element, null); tail = newNode; if (last == null) { head = newNode; } else { last.next = newNode; } size++; returntrue; }
/** * add element before certain element * * @param element * @param target * @return */ privatebooleanaddBefore(T element, Node<T> target) { Node<T> prev = target.prev; Node<T> newNode = newNode<>(prev, element, target); target.prev = newNode; if (prev == null) { head = newNode; } else { prev.next = newNode; } size++; returntrue; }
/** * remove node by element * * @param element * @return */ @Override publicbooleanremove(T element) { if (element == null) { for (Node<T> x = head; x != null; x = x.next) { if (x.element == null) { unlink(x); returntrue; } } } else { for (Node<T> x = head; x != null; x = x.next) { if (element.equals(x.element)) { unlink(x); returntrue; } } } returnfalse; }
/** * remove node by index * * @param index * @return */ @Override public T remove(int index) { return unlink(node(index)); }
/** * get element by index * * @param index * @return */ private Node<T> node(int index) { checkPositionIndex(index); if (index < (size >> 1)) { Node<T> x = head; for (inti=0; i < index; i++) { x = x.next; } return x; } else { Node<T> x = tail; for (inti= size - 1; i > index; i--) { x = x.prev; } return x; } }
/** * unlink node * * @param node */ private T unlink(Node<T> node) { finalTelement= node.element; final Node<T> prev = node.prev; final Node<T> next = node.next;
// if unlink is head if (prev == null) { head = next; } else { prev.next = next; // clear prev node.prev = null; }
// if unlink is tail if (next == null) { tail = prev; } else { next.prev = prev; node.next = null; }
node.element = null; size--; return element; }
privatevoidcheckPositionIndex(int index) { if (index < 0 || index >= size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } }
/** * set element by index * * @param index * @param element * @return */ @Override public T set(int index, T element) { checkPositionIndex(index); Node<T> oldNode = node(index); ToldElement= oldNode.element; oldNode.element = element; return oldElement; }
/** * get element by index * * @param index * @return */ @Override public T get(int index) { Node<T> node = node(index); returnnode== null ? null : node.element; }
/** * clear list */ @Override publicvoidclear() { for (Node<T> x = head; x != null; ) { Node<T> next = x.next; x.element = null; x.next = null; x.prev = null; x = next; } head = tail = null; size = 0; }
/** * contain certain element * * @param element */ @Override publicbooleancontains(T element) { if (element == null) { for (Node<T> x = head; x != null; x = x.next) { if (x.element == null) { returntrue; } } } else { for (Node<T> x = head; x != null; x = x.next) { if (element.equals(x.element)) { returntrue; } } } returnfalse; }
/** * get list size * * @return */ @Override publicintsize() { return size; }
/** * add element * * @param element * @return */ @Override publicbooleanadd(T element) { return addLast(element); }
/** * add element at index * * @param index * @param element * @return */ @Override publicbooleanadd(int index, T element) { if (index < 0 || index > size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } if (index == size) { return add(element); } else { return addBefore(index, element); } }
/** * Add Last element * * @param element * @return */ privatebooleanaddLast(T element) { final Node<T> last = tail; Node<T> newElement = newNode<>(element, head); tail = newElement; if (last == null) { head = newElement; // we need linked itself when add an element at first time tail.next = head; } else { last.next = newElement; } size++; returntrue; }
/** * add element before certain element * * @param element * @param element * @return */ privatebooleanaddBefore(int index, T element) { checkPositionIndex(index); // prev node, start with tail Node<T> prev = tail; Node<T> x = head; for (inti=0; i < index; i++) { prev = x; x = x.next; } // current node Node<T> current = x; // new node Node<T> newNode = newNode<>(element, current); if (index == 0) { head = newNode; } prev.next = newNode; size++; returntrue; }
/** * remove node by element * * @param element * @return */ @Override publicbooleanremove(T element) { // start with tail Node<T> prev = tail; // start with head Node<T> x = head; // start with index -1 intprevIndex= -1;
for (inti=0; i < size; i++) { if (element == null && x.element == null) { break; } if (element != null && element.equals(x.element)) { break; } prev = x; x = x.next; prevIndex = i; }
// if this linked list is empty if (x == null) { returnfalse; }
// if don't match element if (prevIndex == size - 1) { returnfalse; }
Node<T> next = x.next;
// if delete node is head if (prevIndex == -1) { head = next; }
// if delete node is tail if (prevIndex == size - 2) { tail = prev; }
prev.next = next;
size--;
if (size == 0) { head = tail = null; }
// for GC x = null;
returntrue; }
/** * remove element by index * * @param index * @return */ @Override public T remove(int index) { checkPositionIndex(index); Node<T> prev = tail; Node<T> x = head; for (inti=0; i < index; i++) { prev = x; x = x.next; }
// if linked is empty if (x == null) { returnnull; }
Node<T> next = x.next;
// if delete node is head if (index == 0) { head = next; }
// if delete node is tail if (index == size - 1) { tail = prev; }
prev.next = next;
size--;
if (size == 0) { head = tail = null; }
return x.element; }
/** * get element by index * * @param index * @return */ private Node<T> node(int index) { checkPositionIndex(index); Node<T> x = head; for (inti=0; i < index; i++) { x = x.next; } return x; }
privatevoidcheckPositionIndex(int index) { if (index < 0 || index >= size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } }
/** * set element by index * * @param index * @param element * @return */ @Override public T set(int index, T element) { checkPositionIndex(index); Node<T> oldNode = node(index); ToldElement= oldNode.element; oldNode.element = element; return oldElement; }
/** * get element by index * * @param index * @return */ @Override public T get(int index) { return node(index).element; }
/** * clear list element */ @Override publicvoidclear() { for (Node<T> x = head; x != null; ) { Node<T> next = x.next; x.element = null; x.next = null; x = next; } head = tail = null; size = 0; }
/** * contain certain element * * @param element */ @Override publicbooleancontains(T element) { if (head == null) { returnfalse; } Node<T> x = head; for (inti=0; i < size; i++) { if (element == null && x.element == null) { returntrue; } if (element != null && element.equals(x.element)) { returntrue; } x = x.next; } returnfalse; }
/** * get list size * * @return */ @Override publicintsize() { return size; }
/** * add element * * @param element * @return */ @Override publicbooleanadd(T element) { return addLast(element); }
/** * add element at index * * @param index * @param element * @return */ @Override publicbooleanadd(int index, T element) { if (index < 0 || index > size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } if (index == size) { return add(element); } else { return addBefore(index, element); } }
/** * Add last element * * @param element * @return */ privatebooleanaddLast(T element) { Node<T> last = tail; Node<T> newNode = newNode<>(element, last, head); tail = newNode; // add element at first time if (last == null) { head = newNode; tail.next = head; } else { last.next = newNode; } head.prev = tail; size++; returntrue; }
/** * add element before certain element * * @param index * @param element * @return */ privatebooleanaddBefore(int index, T element) { Node<T> target = node(index); Node<T> prev = target.prev; Node<T> newNode = newNode<>(element, prev, target);
prev.next = newNode; target.prev = newNode;
if (index == 0) { head = newNode; }
size++; returntrue; }
/** * remove element * * @param element * @return */ @Override publicbooleanremove(T element) { // start with head Node<T> x = head; // start with index -1 intprevIndex= -1;
for (inti=0; i < size; i++) { if (element == null && x.element == null) { break; } if (element != null && element.equals(x.element)) { break; } x = x.next; prevIndex = i; }
// if this linked list is empty if (x == null) { returnfalse; }
// if don't match element if (prevIndex == size - 1) { returnfalse; }
Node<T> prev = x.prev; Node<T> next = x.next;
// if delete node is head if (prevIndex == -1) { head = next; }
// if delete node is tail if (prevIndex == size - 2) { tail = prev; }
prev.next = next; next.prev = prev;
size--;
if (size == 0) { head = tail = null; }
// for GC x = null;
returntrue; }
/** * remove element by index * * @param index * @return */ @Override public T remove(int index) { checkPositionIndex(index); Node<T> x = head; for (inti=0; i < index; i++) { x = x.next; }
// if linked is empty if (x == null) { returnnull; }
Node<T> prev = x.prev; Node<T> next = x.next;
// if delete node is head if (index == 0) { head = next; }
// if delete node is tail if (index == size - 1) { tail = prev; }
prev.next = next; next.prev = prev;
size--;
if (size == 0) { head = tail = null; }
return x.element; }
privatevoidcheckPositionIndex(int index) { if (index < 0 || index >= size) { thrownewIndexOutOfBoundsException("Index: " + index + ", Size: " + size); } }
/** * set element by index * * @param index * @param element * @return old element */ @Override public T set(int index, T element) { Node<T> oldNode = node(index); ToldElement= oldNode.element; oldNode.element = element; return oldElement; }
/** * get element by index * * @param index * @return */ @Override public T get(int index) { return node(index).element; }
/** * get element by index * * @param index * @return */ private Node<T> node(int index) { checkPositionIndex(index); if (index < (size >> 1)) { Node<T> x = head; for (inti=0; i < index; i++) { x = x.next; } return x; } else { Node<T> x = tail; for (inti= size - 1; i > index; i--) { x = x.prev; } return x; } }
/** * clear list */ @Override publicvoidclear() { for (Node<T> x = head; x != null; ) { Node<T> next = x.next; x.element = null; x.next = null; x = next; } head = tail = null; size = 0; }
/** * contain certain element * * @param element * @return */ @Override publicbooleancontains(T element) { if (head == null) { returnfalse; } Node<T> x = head; for (inti=0; i < size; i++) { if (element == null && x.element == null) { returntrue; } if (element != null && element.equals(x.element)) { returntrue; } x = x.next; } returnfalse; }
/** * get list size * * @return */ @Override publicintsize() { return size; }