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