

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















public E remove(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException();
first remove here (index = 3) null 2 obj
Node nodeBefore = first; for (int i = 0; ; i++) { nodeBefore = nodeBefore.next; } // remove the node … size--; return obj; }
first remove here (index = 3) current
Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node E obj = nodeBefore.next.data; nodeBefore.next = nodeBefore.next.next; size--; return obj; }
Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node Node nodeToRemove = nodeBefore.next; nodeBefore.next = nodeToRemove.next; size--; return nodeToRemove.data; }
private class Node { private E data; private Node prev; private Node next; private Node(E obj, Node previous, Node next){ data = obj; prev = previous; this.next = next; }
null last null first
How would you implement addFirst? null last null first How would you implement addLast?