@Override public E get(int index) { if (index < size()) { Node<E> cursor = head; for (int i = 0; i < index; i++) { cursor = cursor.getNextNode(); } return cursor.getElement(); } else { throw new RuntimeException("Index out of bound."); } }
@Override public int indexOf(E element) { int index = 0; Node<E> cursor = head; while (cursor != null) { if (cursor.getElement() == element) { return index; } cursor = cursor.getNextNode(); index++; } return -1; }
@Override public String toString() { String res = "["; Node<E> cursor = head; while (cursor != null) { res += cursor.getElement(); cursor = cursor.getNextNode(); if (cursor != null) { res += ","; } } res += "]"; return res; }