日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LinkedList源码详解

發布時間:2024/7/19 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LinkedList源码详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, java.io.Serializable

LinkedList繼承自AbstractSequenceList、實現了List及Deque接口。
AbstractSequenceList提供List接口的骨干實現,以減少實現List接口的復雜度。
Deque是Queue的一個子接口,雙向隊列是指該隊列兩端的元素既能入隊(offer)也能出隊(poll)。

  • LinkedList的屬性
transient int size = 0; //存儲元素的個數 transient Node<E> first; //頭節點 //jdk1.6 Entry<E> header = new Entry<E>(null, null, null); transient Node<E> last; //尾節點 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的構造方法
public LinkedList() { //jdk1.6 header.next = header.previous = header;帶有頭結點雙向循環鏈表} //jkd1.7+ 不帶頭節點的雙向鏈表public LinkedList(Collection<? extends E> c) {this();addAll(c); //conllection 轉換成數組 for循環添加}
  • LinkedList的方法
    add方法
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)); //查找index處的節點,把新節點添加到它的前面}private void checkPositionIndex(int index) {if (!isPositionIndex(index)) //非真就拋異常throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private boolean isPositionIndex(int index) {return index >= 0 && index <= size; //當于等于0且小于等于當前size的大小}void linkLast(E e) { //分2種情況解釋 第一次添加 非第一次添加final Node<E> l = last; //第一次添加last為null 第二次添加last不是null final Node<E> newNode = new Node<>(l, e, null); //假如第一次添加個String類型的“1” 第二次添加個String類型的“2”last = newNode; //newNode的item是“1”,newNode.next=null newNode的item是“2”,newNode.next=null if (l == null) //nowNode.prev=null,并將成員變量last指向newNode nowNode.prev="1"first = newNode; //因為是第一次添加,l也是null, 第二次添加,l不是nullelse //將成員變量frist指向newNode 將當前添加的節點指向上一個節點的next屬性l.next = newNode; //添加后是[null]["1"][null] [null]["1"]["2"][null]size++; // prve item next prve item nextmodCount++;}void linkBefore(E e, Node<E> succ) {// assert succ != null;final Node<E> pred = succ.prev; //獲取succ前面的一個節點final Node<E> newNode = new Node<>(pred, e, succ); //新建一個節點,前面指向succ前面一個借點,后面指向succsucc.prev = newNode; //succ前面的節點指向新建的節點if (pred == null) //如果succ前面的節點為空,新建的節點則為頭節點first = newNode;elsepred.next = newNode; //否則就將前面節點的下一點指向新建的節點size++;modCount++;}

remove方法

public boolean remove(Object o) { //判斷要刪除的元素是不是nullif (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 E remove(int index) {checkElementIndex(index); //檢查數組下標越界return unlink(node(index));}private void checkElementIndex(int index) {if (!isElementIndex(index))throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private boolean isElementIndex(int index) {return index >= 0 && index < size;}Node<E> node(int index) {// assert isElementIndex(index);if (index < (size >> 1)) { //假如index小于size的一半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;}}E unlink(Node<E> x) {// assert x != null; //假如LinkerList有這么一組數據[1,2,3,4,5] remove index是0的數據final E element = x.item; //則節點是1 x.item = 1 next = 2 prev = 3 final Node<E> next = x.next;final Node<E> prev = x.prev;if (prev == null) { //prev ==null,2現在就是首節點了first = next; //這里要注意一點,假如有這么一組數據[1,2,3,null,null,null,8,9],remove index是4} else { //item prve next 分別為空 ,只是表示指向的內容是nullprev.next = next; //假如刪除的不是頭結點,將下個節點指向上個節點的下個節點x.prev = null; //將刪除的節點的上個節點指向設置為null}if (next == null) { last = prev;} else { //接著[1,2,3,4,5]來解釋next.prev = prev; //假如刪除的不是尾結點,prve此時是null了,2是首節點,2.prev = null;x.next = null; //將刪除的節點的下個節點指向設置為null}x.item = null; //將刪除的節點也置為nullsize--;modCount++;return element;}

set

public E set(int index, E element) {checkElementIndex(index); //檢查數據下標越界Node<E> x = node(index); //獲取要修改坐標的元素E oldVal = x.item; //獲取這個坐標元素的item屬性x.item = element; //將要修改元素的item屬性引用指向elementreturn oldVal;}

get

public E get(int index) {checkElementIndex(index); //檢查數據下標越界return node(index).item; //for循環來取}

Iterator

private class ListItr implements ListIterator<E> { //正序迭代private Node<E> lastReturned = null;private Node<E> next;private int nextIndex;private int expectedModCount = modCount;ListItr(int index) {next = (index == size) ? null : node(index);nextIndex = index;}public boolean hasNext() {return nextIndex < size;}public E next() {checkForComodification();if (!hasNext())throw new NoSuchElementException();lastReturned = next;next = next.next;nextIndex++;return lastReturned.item;}public boolean hasPrevious() {return nextIndex > 0;}public E previous() {checkForComodification();if (!hasPrevious())throw new NoSuchElementException();lastReturned = next = (next == null) ? last : next.prev;nextIndex--;return lastReturned.item;}public int nextIndex() {return nextIndex;}public int previousIndex() {return nextIndex - 1;}public void remove() {checkForComodification();if (lastReturned == null)throw new IllegalStateException();Node<E> lastNext = lastReturned.next;unlink(lastReturned);if (next == lastReturned)next = lastNext;elsenextIndex--;lastReturned = null;expectedModCount++;}public void set(E e) {if (lastReturned == null)throw new IllegalStateException();checkForComodification();lastReturned.item = e;}public void add(E e) {checkForComodification();lastReturned = null;if (next == null)linkLast(e);elselinkBefore(e, next);nextIndex++;expectedModCount++;}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}}private class DescendingIterator implements Iterator<E> { //倒序迭代private final ListItr itr = new ListItr(size());public boolean hasNext() {return itr.hasPrevious();}public E next() {return itr.previous();}public void remove() {itr.remove();}}

總結

linkedList基于雙向鏈表實現,有序,允許為null,沒有固定容量,不需要擴容。
其中的每個對象包含數據的同時還包含指向鏈表中前一個與后一個元素的引用。

轉載于:https://www.cnblogs.com/Ch1nYK/p/8589415.html

總結

以上是生活随笔為你收集整理的LinkedList源码详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。