前言
推出一個新系列,《看圖輕鬆理解資料結構和演算法》,主要使用圖片來描述常見的資料結構和演算法,輕鬆閱讀並理解掌握。本系列包括各種堆、各種佇列、各種列表、各種樹、各種圖、各種排序等等幾十篇的樣子。
雙向連結串列
雙向連結串列屬於連結串列的一種,也叫雙連結串列雙向即是說它的連結方向是雙向的,它由若干個節點組成,每個節點都包含下一個節點和上一個節點的指標,所以從雙向連結串列的任意節點開始,都能很方便訪問他的前驅結點和後繼節點。
雙連結串列特點
- 建立雙連結串列時無需指定連結串列的長度。
- 比起單連結串列,雙連結串列需要多一個指標用於指向前驅節點,所以需要儲存空間比單連結串列多一點。
- 雙連結串列的插入和刪除需要同時維護 next 和 prev 兩個指標。
- 雙連結串列中的元素訪問需要通過順序訪問,即要通過遍歷的方式來尋找元素。
雙連結串列建立
建立一個空連結串列,

插入鏈尾
將the monster is coming
這些單詞按順序分別插入尾部,建立“the”節點,

連線起來,

建立“monster”節點,

再連線起來,

以此類推,將剩下的節點全部建立並連線起來。


建立迭代器
迭代器的 current 指標初始指向head,

執行兩次 next 操作, current 指標指向索引為2的節點,

此時的節點值為,

設定 current 指標指向索引為3的節點,

插入節點
在索引1後面插入“big”節點。先將 current 指標指向索引為1的節點,建立一個"big"新節點,

插入到 current 指向位置,

刪除節點
將“big”節點刪除,移動當前指標 current 到“but”節點位置,

執行刪除操作,斷掉“big”節點與前後兩節點的 next 和 prev 指標,然後將“the”節點與“monster”節點關聯起來,


雙向迴圈連結串列
前面的雙向連結串列的 head 節點和鏈尾沒有連線關係,所以如果要訪問最後一個節點的話需要從頭開始遍歷,直到最後一個節點。在雙向連結串列基礎上改進一下,把 header 節點的 prev 指標指向最後一個節點,而最後一個節點的 next 指標指向 header 節點,於是便構成雙向迴圈連結串列。

-------------推薦閱讀------------
我的開源專案彙總(機器&深度學習、NLP、網路IO、AIML、mysql協議、chatbot)
跟我交流,向我提問:

歡迎關注:
