Java基礎(chǔ)之容器LinkedList
一、LinkedList的整體結(jié)構(gòu)
1.1、LinkedList的繼承關(guān)系
- public class LinkedList<E> extends AbstractSequentialList <E> implements List<E>, Deque<E>
- LinkedList具備AbstractSequentialList的特點(diǎn):AbstractSequentialList 只支持按次序訪問,而不像 AbstractList 那樣支持隨機(jī)訪問
- LinkedList具備List的特點(diǎn)
- LinkedList具備Deque的特點(diǎn):Deque是一個(gè)線性collection,支持在兩端插入和移除元素
1.2、LinkedList的結(jié)構(gòu)
//元素個(gè)數(shù) transient int size = 0; //第一個(gè)元素指針 transient Node<E> first; //最后一個(gè)元素指針 transient Node<E> last; //Node節(jié)點(diǎn)的結(jié)構(gòu) private static class Node<E> { E item;//當(dāng)前元素 Node<E> next;//當(dāng)前元素的下一個(gè)指針 Node<E> prev;//當(dāng)前元素的上一個(gè)指針 Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
1.2.1 Node的結(jié)構(gòu)
LinkedList結(jié)構(gòu)
LinkedList特點(diǎn)
1.LinkedList是通過雙鏈表去實(shí)現(xiàn)的。
2.LinkedList不存在容量不足的問題,因?yàn)槭擎湵怼?/p>
3.LinkedList實(shí)現(xiàn)了Deque,而Deque接口定義了在雙端隊(duì)列兩端訪問元素的方法,所以LinkedList可以作為FIFO(先進(jìn)先出)的隊(duì)列;LinkedList可以作為LIFO(后進(jìn)先出)的棧
二、源碼分析
2.1、添加元素
//添加元素 public boolean add(E e) { //默認(rèn)調(diào)用,尾部添加元素的方法 linkLast(e); return true; } //尾部添加元素 void linkLast(E e) { //記錄當(dāng)前尾部元素 final Node<E> l = last; //創(chuàng)建一個(gè)新的Node節(jié)點(diǎn) ,prev是當(dāng)前的尾節(jié)點(diǎn),next指向null final Node<E> newNode = new Node<>(l, e, null); //將last設(shè)置為新節(jié)點(diǎn) last = newNode; //判斷當(dāng)前尾部節(jié)點(diǎn)是否為null if (l == null) //當(dāng)前尾部節(jié)點(diǎn)為null,就掛到頭結(jié)點(diǎn)上 first = newNode; else //當(dāng)前尾部節(jié)點(diǎn)不為null,就將新建的Node掛到當(dāng)前l(fā)ast節(jié)點(diǎn)的next指針上 l.next = newNode; //元素的個(gè)數(shù)+1 size++; //LinkedList修改記錄+1 modCount++; }
新增元素add()方法默認(rèn)是尾部追加,核心就是將新建的Node節(jié)點(diǎn)追加到當(dāng)前l(fā)ast節(jié)點(diǎn)的next指針上 ,偽代碼:
Node newNode=new Node(); newNode.prev=last; last.next=newNode; last=newNode; last.next=null;
addFirst:首部追加
public void addFirst(E e) { linkFirst(e); } //頭部追加 private void linkFirst(E e) { //記錄當(dāng)前首部元素 final Node<E> f = first; //新建一個(gè)Node節(jié)點(diǎn) final Node<E> newNode = new Node<>(null, e, f); //首部元素指向新建的節(jié)點(diǎn) first = newNode; //判斷當(dāng)前首部指針是否為null if (f == null) //當(dāng)前首部指針為null,就把新建的節(jié)點(diǎn)掛到last指針上 last = newNode; else //當(dāng)前首部指針不為null,就把新建的節(jié)點(diǎn)掛到,當(dāng)前first指針指向元素的prev指針上 f.prev = newNode; //元素個(gè)數(shù)+1 size++; //LinkedList修改記錄+1 modCount++; }
首部追加的邏輯與尾部追加基本相同,偽代碼:
Node newNode=new Node(); newNode.next=first; first.prev=newNode; first=newNode; first.prev=null;(也可以:newNode.prev=null)
指定位置添加元素:add(int index, E element):
public void add(int index, E element) { //檢查要插入的位置是否合法 checkPositionIndex(index); //如要插入的位置在最后,直接調(diào)用linkLast() if (index == size) linkLast(element); else //如要插入的位置不在最后,就先查找再插入 linkBefore(element, node(index)); } //查找要插入元素的位置 Node<E> node(int index) { // assert isElementIndex(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; } } //將新建的元素插入到查找的元素前面 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++; }
LinkedList是一個(gè)雙向鏈表,他只記錄了頭部和尾部位置,如果我們要指定位置插入,他會(huì)這么做:
1.先遍歷查找出要插入的元素位置,然后再插入;查找方式是根據(jù) index < (size >> 1) 判斷結(jié)果,決定是從頭遍歷,還是從尾部遍歷,這種遍歷方式類似于二分查找(只在第一層循環(huán)二分)
2.新建一個(gè)Node節(jié)點(diǎn),插入到查找出來的元素的前面
由此可知為何鏈表對隨機(jī)位置讀寫是不合適的;他的時(shí)間復(fù)雜度=O(n/2) ,如果n很大,我們一般就認(rèn)為他的時(shí)間復(fù)雜度=O(n)
2.2、刪除元素
//重寫List的remove() public boolean remove(Object o) { if (o == null) { //如果要?jiǎng)h除的元素null元素,從頭開始查找這個(gè)null元素 for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { //如果要?jiǎng)h除的元素不null元素,從頭開始查找這個(gè)非null元素 for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } //執(zhí)行刪除邏輯,實(shí)質(zhì)就是打斷改元素與鏈表的引用關(guān)系 E unlink(Node<E> x) { // assert x != null; //記錄改元素的值,實(shí)際作用就是做返回值 final E element = x.item; //記錄當(dāng)前元素的下一個(gè)節(jié)點(diǎn) final Node<E> next = x.next; //記錄當(dāng)前元素的上一個(gè)節(jié)點(diǎn) final Node<E> prev = x.prev; //判斷 x->prev 節(jié)點(diǎn)是否為null,為null就是刪除頭結(jié)點(diǎn) if (prev == null) { first = next; } else { //將 x->prev節(jié)點(diǎn)的next指針指向x節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn) prev.next = next; //將 x->prev 指針,設(shè)置為null(斷開引用關(guān)系) x.prev = null; } //判斷 x->next 節(jié)點(diǎn)是否為null,為null就是刪尾部結(jié)點(diǎn) if (next == null) { last = prev; } else { //將x->next節(jié)點(diǎn)的prev指針指向x->prev next.prev = prev; //將 x->next指針,設(shè)置為null(斷開引用關(guān)系) x.next = null; } //將x的值設(shè)置為null x.item = null; //集合大小-1 size--; //集合的修改記錄-1 modCount++; return element; }
這里我們看到LinkedList重寫了List的remove方法,整個(gè)刪除邏輯也是先查找再刪除,時(shí)間復(fù)雜度O(n),如果是刪除首部元素時(shí)間復(fù)雜度=O(1),若要?jiǎng)h除尾部元素請使用removeLast( )
- LinkedLis刪除首部元素:removeFirst()
- LinkedLis刪除尾部元素:removeLast()
- LinkedLis首部出隊(duì):pollFirst( ) ,隊(duì)列的特點(diǎn)
- LinkedLit尾部出隊(duì):pollLast( ),隊(duì)列的特點(diǎn)
2.3、迭代器
Iterator迭代器只能是從頭往尾迭代,而LinkedList是雙向鏈表,他還可以從尾往頭部迭代,JAVA提供了一個(gè)新的迭代器接口:
public interface ListIterator<E> extends Iterator<E>{ //判斷是否存在下一個(gè)元素 boolean hasNext(); //獲取下一個(gè)元素 E next(); //判斷是否還有前一個(gè)元素 boolean hasPrevious(); //獲取前一個(gè)元素 E previous(); }
LinkedList實(shí)現(xiàn)該接口:
private class ListItr implements ListIterator<E> { private Node<E> lastReturned;//上一次next() 或者 previous()的元素 private Node<E> next;//lastReturned->next 指向的元素 private int nextIndex;//下一個(gè)元素的位置 private int expectedModCount = modCount; }
LinkedList從前往后遍歷:
//是否存在下一個(gè)元素 public boolean hasNext() { return nextIndex < size; } public E next() { //檢查集合的版本 checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); //lastReturned賦值上次next lastReturned = next; //next賦值為上次next->next next = next.next; //下一個(gè)元素的位置 nextIndex++; return lastReturned.item; }
LinkedList從后往前遍歷:
//判斷是否到頭了 public boolean hasPrevious() { return nextIndex > 0; } //從尾部往頭部取數(shù)據(jù) public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); // next==null:第一次遍歷取尾節(jié)點(diǎn)(last),或者上一次遍歷時(shí)把尾節(jié)點(diǎn)刪除掉了 // next!=null:已經(jīng)發(fā)生過遍歷了,直接取前一個(gè)節(jié)點(diǎn)即可(next.prev) lastReturned = next = (next == null) ? last : next.prev; //遍歷的指針-1 nextIndex--; return lastReturned.item; }
迭代器刪除元素:
public void remove() { checkForComodification(); // lastReturned 是本次迭代需要?jiǎng)h除的值 // lastReturned==null則調(diào)用者沒有主動(dòng)執(zhí)行過 next() 或者 previos(),二直接調(diào)remove() // lastReturned!=null,是在上次執(zhí)行 next() 或者 previos()方法時(shí)賦的值 if (lastReturned == null) throw new IllegalStateException(); //保存將當(dāng)前要?jiǎng)h除節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)(如果是從尾往頭遍歷,該值永遠(yuǎn)是null) Node<E> lastNext = lastReturned.next; //刪除當(dāng)前節(jié)點(diǎn) unlink(lastReturned); // next == lastReturned:從尾到頭遞歸順序,并且是第一次迭代,并且要?jiǎng)h除最后一個(gè)元素的情況下, // previous() 方法里面設(shè)置了 lastReturned = next = last,所以 next 和 lastReturned會(huì)相等 if (next == lastReturned) next = lastNext; else nextIndex--; lastReturned = null; expectedModCount++; }
三、總結(jié)
LinkedList底層數(shù)據(jù)結(jié)構(gòu)是雙向鏈表,所以他更適合順序操作,由于他繼承了Deque接口,同時(shí)他具有隊(duì)列的性質(zhì),非線程安全的集合
到此這篇關(guān)于Java基礎(chǔ)之容器LinkedList的文章就介紹到這了,更多相關(guān)Java容器LinkedList內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)電話本管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JAVA實(shí)現(xiàn)電話本管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Spring MVC傳遞接收參數(shù)方式小結(jié)
大家在開發(fā)中經(jīng)常會(huì)用到Spring MVC Controller來接收請求參數(shù),主要常用的接收方式就是通過實(shí)體對象以及形參等方式、有些用于GET請求,有些用于POST請求,有些用于兩者,下面介紹幾種常見的Spring MVC傳遞接收參數(shù)的方式2021-11-11Java的Struts2框架配合Ext JS處理JSON數(shù)據(jù)的使用示例
這篇文章主要介紹了Java的Struts2框架配合Ext JS處理JSON數(shù)據(jù)的使用示例,包括將Ext JS中的JSON數(shù)據(jù)解析為列表的方法,需要的朋友可以參考下2016-03-03淺談Spring學(xué)習(xí)之request,session與globalSession作用域
這篇文章主要介紹了Spring學(xué)習(xí)之request,session與globalSession作用域的相關(guān)內(nèi)容,需要的朋友可以參考下。2017-09-09解決IDEA 左側(cè)Project中沒有out文件夾的問題
這篇文章主要介紹了解決IDEA 左側(cè)Project中沒有out文件夾的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02IntelliJ IDEA下Maven創(chuàng)建Scala項(xiàng)目的方法步驟
這篇文章主要介紹了IntelliJ IDEA下Maven創(chuàng)建Scala項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06