LinkedList is an implementation of List, backed by a linked list. All optional operations are supported, adding, removing and replacing. The elements can be any objects. @since 1.2
| 25 | * @since 1.2 |
| 26 | */ |
| 27 | public class LinkedList<E> extends AbstractSequentialList<E> implements |
| 28 | List<E>, Deque<E> { |
| 29 | |
| 30 | |
| 31 | transient int size = 0; |
| 32 | |
| 33 | transient Link<E> voidLink; |
| 34 | |
| 35 | private static final class Link<ET> { |
| 36 | ET data; |
| 37 | |
| 38 | Link<ET> previous, next; |
| 39 | |
| 40 | Link(ET o, Link<ET> p, Link<ET> n) { |
| 41 | data = o; |
| 42 | previous = p; |
| 43 | next = n; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private static final class LinkIterator<ET> implements ListIterator<ET> { |
| 48 | int pos, expectedModCount; |
| 49 | |
| 50 | final LinkedList<ET> list; |
| 51 | |
| 52 | Link<ET> link, lastLink; |
| 53 | |
| 54 | LinkIterator(LinkedList<ET> object, int location) { |
| 55 | list = object; |
| 56 | expectedModCount = list.modCount; |
| 57 | if (0 <= location && location <= list.size) { |
| 58 | // pos ends up as -1 if list is empty, it ranges from -1 to |
| 59 | // list.size - 1 |
| 60 | // if link == voidLink then pos must == -1 |
| 61 | link = list.voidLink; |
| 62 | if (location < list.size / 2) { |
| 63 | for (pos = -1; pos + 1 < location; pos++) { |
| 64 | link = link.next; |
| 65 | } |
| 66 | } else { |
| 67 | for (pos = list.size; pos >= location; pos--) { |
| 68 | link = link.previous; |
| 69 | } |
| 70 | } |
| 71 | } else { |
| 72 | throw new IndexOutOfBoundsException(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public void add(ET object) { |
| 77 | if (expectedModCount == list.modCount) { |
| 78 | Link<ET> next = link.next; |
| 79 | Link<ET> newLink = new Link<ET>(object, link, next); |
| 80 | link.next = newLink; |
| 81 | next.previous = newLink; |
| 82 | link = newLink; |
| 83 | lastLink = null; |
| 84 | pos++; |
nothing calls this directly
no outgoing calls
no test coverage detected