Java LinkedList??????
???????????? ???????[ 2016/1/29 14:09:48 ] ??????????????????? ???????
???????
????LinkedList ??????????????????????洢???? LinkedList ?????? ArrayList ???????????????????? ArrayList ??????????鱣???????????????????????????????????????????????????????????????? LinkedList ????????????????洢?????????????????????????????????????????臨????? O(1) (??????????????????????????)????????? LinkedList ?????????
??????й??
????public class LinkedList<E>
????extends AbstractSequentialList<E>
????implements List<E>?? Deque<E>?? Cloneable?? java.io.Serializable
????LinkedList ?????????????? AbstractSequentialList ?????????????? ListIterator ???????????????????? add ??????
????public void add(int index?? E element) {
????try {
????listIterator(index).add(element);
????} catch (NoSuchElementException exc) {
????throw new IndexOutOfBoundsException("Index: "+index);
????}
????}
??????????Щ?????? LinkedList ?б???д???
????LinkedList ????? List ?? Deque ?? Cloneable ??? Serializable ???????? Deque ???????н??????? LinkedList ???????????????л?????????С?
???????????
????transient int size = 0;
????transient Node<E> first;
????transient Node<E> last;
???????????????????? size ?????????? first ?????????????????? last ?????????????????????????? Node ????????????????????????????
????private static class Node<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;
????}
????}
??????????????????????????????????????????????????????
??????з???
????LinkedList ????м??????????з????????????????????????????????????????????
????private void linkFirst(E e) {
????final Node<E> f = first; //????浱?????
????//????????????????e?????????????????????????
????final Node<E> newNode = new Node<>(null?? e?? f);
????first = newNode; //??first???????
????if (f == null) //????????????????last??????????
????last = newNode;
????else · //?????????????????????μ?????
????f.prev = newNode;
????size++;
????modCount++;
????}
??????????????????????????????????????臨???? O(1) ??????????????ú?????????????????м?????????????
????//β??????
????void linkLast(E e) {
????final Node<E> l = last;
????final Node<E> newNode = new Node<>(l?? e?? null);
????last = newNode;
????if (l == null) //????????????????first??????????
????first = newNode;
????else
????l.next = newNode;
????size++;
????modCount++;
????}
????//?м????
????void linkBefore(E e?? Node<E> succ) {
????// assert succ != null;
????final Node<E> pred = succ.prev;
????final Node<E> newNode = new Node<>(pred?? e?? succ);
????succ.prev = newNode;
????if (pred == null)
????first = newNode;
????else
????pred.next = newNode;
????size++;
????modCount++;
????}
????//???????
????private E unlinkFirst(Node<E> f) {
????// assert f == first && f != null;
????final E element = f.item;
????final Node<E> next = f.next; //?????????????
????f.item = null;
????f.next = null; // help GC
????first = next; //??first???????????
????if (next == null) //??????????????????????????????????????????????????last???null
????last = null;
????else //???????????????????????null
????next.prev = null;
????size--;
????modCount++;
????return element;
????}
????//???β???
????private E unlinkLast(Node<E> l) {
????// assert l == last && l != null;
????final E element = l.item;
????final Node<E> prev = l.prev; //???????????
????l.item = null;
????l.prev = null; // help GC
????last = prev; //last??????????
????if (prev == null) //???????????????ж???????
????first = null;
????else
????prev.next = null;
????size--;
????modCount++;
????return element;
????}
????//???????м???????
????E unlink(Node<E> x) {
????// assert x != null;
????final E element = x.item;
????final Node<E> next = x.next; //??????????
????final Node<E> prev = x.prev; //????????
????if (prev == null) { //?????????????????????first????????????
????first = next;
????} else { //???????????????????????????????????
????prev.next = next;
????x.prev = null;
????}
????if (next == null) { //?ж????????????????????????????????
????last = prev;
????} else {
????next.prev = prev;
????x.next = null;
????}
????x.item = null;
????size--;
????modCount++;
????return element;
????}
????????????
???????????????????????????漸??????????????? add ??????
????public boolean add(E e) {
????linkLast(e);
????return true;
????}
????public boolean add(E e) {
????linkLast(e);
????return true;
????}
????public void add(int index?? E element) {
????checkPositionIndex(index);
????if (index == size)
????linkLast(element);
????else
????linkBefore(element?? node(index));
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11