
public class LinkedListextends AbstractSequentialList
implements List, Deque, Cloneable, java.io.Serializable
LinkedList是非线程安全的
1 2 3 4 5 6 7 8 9 10 11 12 13
|
private static class <E> { E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
|
public E getFirst() //获取第一个元素
1 2 3 4 5 6 7
|
public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; }
|
public E getLast() //获取最后一个元素
1 2 3 4 5 6 7
|
public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; }
|
public E removeFirst()//移除第一个元素
1 2 3 4 5 6 7
|
public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
private E unlinkFirst(Node<E> f) { final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
|
public E removeLast()//移除List最后一个元素
1 2 3 4 5 6
|
public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
private E unlinkLast(Node<E> l) { final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; }
|
public void addFirst(E e)//将元素添加到List队首
1 2 3
|
public void addFirst(E e) { linkFirst(e); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
private void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; }
|
public void addLast(E e) //将元素e添加到队尾
1 2 3
|
public void addLast(E e) { linkLast(e); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }
|
public boolean contains(Object o) //判断list是否包含元素o
1 2 3
|
public boolean contains(Object o) { return indexOf(o) != -1; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
|
public boolean remove(Object o)//移除元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public boolean remove(Object o) { if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
|
public boolean addAll(Collection<? extends E> c) //添加多个元素
1 2 3
|
public boolean addAll(Collection<? extends E> c) { return addAll(size, c); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<E> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; } for (Object o : a) { ("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; } if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Node<E> node(int index) { if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
|
近期评论