国产精品电影_久久视频免费_欧美日韩国产激情_成年人视频免费在线播放_日本久久亚洲电影_久久都是精品_66av99_九色精品美女在线_蜜臀a∨国产成人精品_冲田杏梨av在线_欧美精品在线一区二区三区_麻豆mv在线看

硬核復刻 Redis 底層雙向鏈表核心實現

開源 Redis
本文將介紹一下筆者的開源項目mini-redis中對于鏈表的復刻思路,希望對你閱讀我們的項目源碼有所幫助。

1.構建雙向鏈表基架

redis中雙向鏈表的節點都是由如下3個元素構成:

  • 指向前驅節點的指針prev。
  • 指向后繼節點的指針next。
  • 指向當前節點值的指針value。

所以筆者對于雙向鏈表節點的結構體的定義也按照這套定義復刻:

// Definition of the listNode structure for a doubly linked list
type listNode struct {
 //Node pointing to the previous node of the current node.
 prev *listNode
 //Node pointing to the successor node of the current node.
 next *listNode
 //Record information about the value stored in the current node.
 value *interface{}
}

因為是雙向鏈表,這意味著鏈表可以從前或者從后進行鏈表操作,所以雙向鏈表就必須具備如下3個構成部分:

  • 指向鏈表第一個節點的head指針。
  • 指向鏈表最后一個節點的tail指針。
  • 維護鏈表長度的字段len。

于是我們基于這個思路,再次給出鏈表的結構體定義:

type list struct {
 //Points to the first node of the doubly linked list
 head *listNode
 //points to the last node of the linked list.
 tail *listNode
 //Record the current length of the doubly linked list
 len int64
}

了解了基礎的結構定義,我們就可以編寫雙向鏈表初始化的函數listCreate,和redis初始化步驟基本一致,筆者同樣是按照:結構體內存空間分配、頭尾指針初始化、長度設置為0,然后返回這個雙向鏈表結構體指針的步驟進行操作:

func listCreate() *list {
 //Allocate memory space for the doubly linked list
 var l *list
 l = new(list)
 
 //Initialize the head and tail pointers.
 l.head = nil
 l.tail = nil
 
 //Initialize the length to 0, indicating that the current linked list has no nodes
 l.len = 0
 
 return l
}

實現節點頭插和尾后追加

此時,我們就可以實現mini-redis中雙向鏈表的第一個操作——頭插法,該操作就是將新插入的節點作為鏈表的頭節點,該操作的步驟比較明確:

  • 新節點指向原有頭節點。
  • 原有頭節點的前驅指針指向新節點。
  • 將head指針指向新節點,完成節點頭插。

完成這些操作之后,維護一下鏈表長度信息:

基于上述思路筆者給出對應的實現,和原生redis的函數和入參基本一致,傳入需要操作的鏈表和value值之后,將value封裝為節點,結合上述的思路將其設置為鏈表頭節點:

func listAddNodeHead(l *list, value *interface{}) *list {
 //Allocate memory for a new node and set its value.
 var node *listNode
 node = new(listNode)
 node.value = value
 //If the length is 0, then both the head and tail pointers point to the new node.
 if l.len == 0 {
  l.head = node
  l.tail = node
 } else {
  //Make the original head node the successor node of the new node, node.
  node.prev = nil
  node.next = l.head
  l.head.prev = node
  l.head = node
 }
 //Maintain the information about the length of the linked list.
 l.len++
 return l
}

與之同理的還有尾插法,無論入參和操作步驟基本一致,唯一區別就是將節點追加到鏈表末端作為尾節點,讀者可以參考筆者的的實現和注釋了解操作細節:

func listAddNodeTail(l *list, value *interface{}) *list {
 //Allocate memory for a new node and set its value.
 var node *listNode
 node = new(listNode)
 node.value = value
 //If the length is 0, then both the head and tail pointers point to the new node.
 if l.len == 0 {
  l.head = node
  l.tail = node
 } else {
  //Append the newly added node after the tail node to become the new tail node.
  node.prev = l.tail
  node.next = nil
  l.tail.next = node
  l.tail = node
 }
 //Maintain the information about the length of the linked list.
 l.len++
 return l

}

基于索引定位節點

雙向鏈表支持基于索引的方式查詢,例如我們希望查詢索引2節點的值,傳入index為2,雙向鏈表就會基于索引2這個值跳越兩次來到目標節點并返回:

假如我們傳入負數,例如負數2,按照語義就是返回倒數第2個節點,雙向鏈表會按照公式(-index)-1得到值1,然后從尾節點跳1步找到目標節點并返回:

對此我們給出相應的源碼實現,整體思路和上述說明一致,讀者可參考源碼和注釋了解細節:

func listIndex(l *list, index int64) *listNode {
 var n *listNode
 //"If less than 0, calculate the index value as a positive number n,
 //then continuously jump to the node pointed to by prev based on this positive number n.
 if index < 0 {
  index = (-index) - 1
  n = l.tail

  for index > 0 && n != nil {
   n = n.prev
   index--
  }
 } else {
  //Conversely, walk n steps from the front and reach the target node via next, then return.
  n = l.head
  for index > 0 && n != nil {
   n = n.next
   index--
  }
 }

 return n
}

指定位置插入

雙向鏈表支持在指定元素的前面或者后面插入元素,我們以元素后插入為例,雙向鏈表會將新節點追加到原有節點后面并維護前驅后繼指針的信息,插入到指定節點的前方也是同理:

唯一需要注意的就是如果新節點追加到尾節點后面,我們需要將tail指向新節點。追加到頭節點同理,我們需要將head指針指向新節點:

對此我們給出listInsertNode的源碼實現,讀者可參閱思路并結合注釋了解實現細節:

func listInsertNode(l *list, old_node *listNode, value *interface{}, after bool) *list {
 //Allocate memory for a new node and set its value.
 var node *listNode
 node = new(listNode)
 node.value = value
 //If after is true, insert the new node after the old node.
 if after {
  node.prev = old_node
  node.next = old_node.next
  //If the old node was originally the tail node, after the modification,
  //make the node the new tail node.
  if l.tail == old_node {
   l.tail = node
  }
 } else {
  //Add the new node before the old node.
  node.next = old_node
  node.prev = old_node.prev
  //If the original node is the head, then set the new node as the head
  if l.head == old_node {
   l.head = node

  }
 }
 //If the node's predecessor node is not empty, then point the predecessor to the node.
 if node.prev != nil {
  node.prev.next = node
 }
 //If the node's successor node is not empty, make this successor point to the node.
 if node.next != nil {
  node.next.prev = node
 }
 //Maintain the information about the length of the linked list.
 l.len++
 return l

}

雙向鏈表節點刪除

節點刪除則比較簡單,傳入要被刪除的節點指針,讓被刪除節點d的前驅節點指向d的后繼節點,同時讓d的后繼指向d的前驅:

唯一需要注意的就是以下兩種情況:

  • 刪除的是頭節點,則讓head指向頭節點的后面一個節點。
  • 刪除的是尾節點,則讓tail指向尾節點的前一個節點。

最后我們斷掉被刪除節點的前后繼指針指向,讓go語言垃圾回收自動幫我們完成節點刪除即可,這里我們也給出相應的源碼實現:

func listDelNode(l *list, node *listNode) {
 //If the predecessor node is not empty,
 //then the predecessor node's next points to the successor node of the node being deleted
 if node.prev != nil {
  node.prev.next = node.next
 } else {
  //If the deleted node is the head node, set the head to point to the next node.
  l.head = node.next
 }

 //If next is not empty, then let next point to the node before the deleted node
 if node.next != nil {
  node.next.prev = node.prev
 } else {
  //If the deleted node is the tail node, make 
  //the node before the deleted node the new tail node.
  l.tail = node.prev
 }
 //help gc
 node.prev = nil
 node.next = nil

 l.len--

}
責任編輯:趙寧寧 來源: 寫代碼的SharkChili
相關推薦

2024-11-04 06:00:00

redis雙向鏈表

2021-05-07 08:20:52

前端開發技術熱點

2020-07-01 08:07:33

Redis

2020-05-27 20:45:31

Redis底層數據

2023-10-16 23:12:02

Redis數據結構

2021-11-02 09:05:25

Redis

2025-04-25 11:00:00

mini-redisRedisINCR指令

2022-12-26 00:51:33

雙向鏈表二叉搜索樹

2020-07-07 07:34:29

RedisSDS數據結構

2020-12-17 08:03:57

LinkedList面試源碼

2022-11-11 10:48:55

AQS源碼架構

2021-12-07 06:55:17

二叉搜索樹鏈表

2025-06-23 10:13:00

FutureTask線程開發

2024-04-26 00:02:00

Rust語言LinkedList

2023-01-04 07:54:03

HashMap底層JDK

2025-04-07 11:10:00

Python列表開發

2021-01-22 09:47:22

鴻蒙HarmonyOS應用開發

2025-08-26 02:15:00

Redis字符串)SDS

2020-07-03 13:29:08

Redis集群哈希槽

2020-10-21 09:17:52

Redis面試內存
點贊
收藏

51CTO技術棧公眾號

在线中文字幕日韩| 99在线精品免费视频九九视| 欧美国产视频在线| 狠狠色噜噜狠狠狠狠色吗综合| 凹凸国产熟女精品视频| 成人免费在线观看av| 亚洲人成网在线播放| 全色精品综合影院| 成人精品视频网站| 日韩一区二区在线观看视频| 国产xxxxx视频| 免费日韩精品中文字幕视频在线| 一区二区三区四区在线免费观看| 在线观看成人av电影| 五月天激情在线| 久久久久91| 久久精品国产成人一区二区三区 | 久久久国产精品网站| 精品人伦一区二区三区蜜桃网站 | 亚洲女人av| 国产有码一区二区| 欧美韩一区二区| 日韩中文字幕精品| 美女福利一区二区| 国产成人午夜精品影院观看视频 | 国产精品免费视频网站| 国语自产精品视频在免费| 国产中文字幕视频在线观看| 日日摸夜夜添夜夜添国产精品| 97在线中文字幕| 日本电影一区二区| 91精品国产色综合| 一级毛片在线看| 欧美日韩网址| 国产精品伦子伦免费视频| 毛片网站在线观看| 一区二区三区精品视频| 91亚洲精华国产精华| 亚州综合一区| 日韩免费视频在线观看| 国产成人精品免费视| 国产成人jvid在线播放| 国产精品45p| 91在线直播亚洲| 免费日韩电影| 91久久线看在观草草青青| 久久国产主播精品| 毛片毛片毛片毛片| 高清久久精品| 中文字幕一区免费在线观看| 亚洲大胆人体av| 在线视频国内自拍亚洲视频| 人体精品一二三区| 一区二区三区.www| 国产精品999视频| 国产成人激情小视频| www免费在线观看| 国产精品亚洲欧美日韩一区在线| 欧美午夜美女看片| 久热99视频在线观看| 嫩草一区二区三区| 日本精品视频在线| 国产精品美女久久久久久不卡| 国产极品精品在线观看| 久久久久久免费视频| 美女一区视频| 在线手机中文字幕| 亚洲欧美日韩中文在线制服| 成人午夜剧场免费观看完整版| 国产精品色在线| 在线成年人视频| 欧美一区二区日韩一区二区| xxxxxx欧美| 国外视频精品毛片| 中文字幕一区二区三区乱码图片| 欧美日韩精品久久久免费观看| 狠狠狠色丁香婷婷综合激情| 97久久精品国产| 国产毛片在线| 久久网这里都是精品| 成人漫画网站免费| 色欧美乱欧美15图片| 在线观看操人| 亚洲电影在线免费观看| 国产精品一区二区三区四区色| 精品国产免费人成电影在线观看四季| 四虎视频在线精品免费网址| 国产美女91呻吟求| 国产午夜久久av| 国产视频福利一区| 国内外成人在线视频| 丁香视频免费观看| fc2成人免费人成在线观看播放| 国产99在线免费| 韩国一区二区在线观看| 最近最好的中文字幕2019免费 | 青青青草原在线| 日韩视频在线一区二区| 综合中文字幕| 亚洲护士老师的毛茸茸最新章节 | 美女网站视频在线| 欧美国产日韩一区二区在线观看| 一区二区高清| 人妻无码视频一区二区三区| 欧美日韩久久一区| 视频一区视频二区欧美| 欧美人xxxxx| 伊人色综合久久天天| 精品91久久| 成人午夜电影在线播放| 欧美国产激情一区二区三区蜜月| av网站在线免费播放| 性欧美办公室18xxxxhd| 免费一区二区视频| 久久99欧美| 国产日产精品1区| 欧美激情20| 国产精品综合久久久久久| 久久久综合色| 熟女视频一区二区三区| 色婷婷综合久久久久中文| 日韩欧美久久| 亚洲春色在线| 欧美视频一区二| 欧美日韩精品一区二区三区在线观看| 中日韩在线视频| 欧美三级三级三级| 一区二区三区日本久久久| 97视频在线免费| 丁香婷婷综合网| 青青影院在线观看| 国产精品亚洲网站| 国产欧美日韩在线看| 蜜桃麻豆影像在线观看| 国产呦系列欧美呦日韩呦| 成人高清dvd| 免费99视频| 欧美写真视频网站| 91视频久久| 在线看的你懂得| 精品国产一区二区精华| 男女免费观看在线爽爽爽视频| 一本色道久久综合亚洲精品婷婷 | 精品国产麻豆免费人成网站| 在线欧美成人| 高清欧美性猛交xxxx| 亚洲少妇中文在线| 国产精品大陆在线观看| 日韩一区二区三区免费播放| 麻豆亚洲一区| 快播亚洲色图| 男插女免费视频| 男人天堂网视频| 欧美精品性生活| 91高潮精品免费porn| 国产精品中文字幕在线| 男女无套免费网站| 黄色av免费在线| 国产高清自拍一区| 玖玖国产精品视频| 蜜臀av.com| 欧美午夜理伦三级在线观看| 欧美自拍丝袜亚洲| 91精品国产色综合久久不卡蜜臀 | 亚洲欧洲高清在线| 欧美资源在线观看| 精品国产福利在线| 久久久999国产| 国产精品99免视看9| 久久久久一区二区| 国产免费裸体视频| 亚洲成人国产精品| 国产成+人+综合+亚洲欧洲| 欧美综合在线观看| 国产成人精品av在线| 人禽交欧美网站免费| 成人黄色免费网站在线观看| 老鸭窝91久久精品色噜噜导演| 麻豆精品久久| 999久久精品| 久久久久久久久久久免费视频| 青青在线免费观看视频| 最新97超碰在线| 人人爱人人干婷婷丁香亚洲| 天堂av最新在线| 免费在线国产视频| 久久精品九色| 久久久99久久精品欧美| 久久视频在线直播| 亚洲aⅴ日韩av电影在线观看| 久久大香伊蕉在人线观看热2| 国产黄色一级网站| 色多多视频在线观看| 国产日韩欧美综合| 中国人体摄影一区二区三区| 成人在线视频免费| 国产一区日韩一区| 精品久久久久久久久久久久久久| 国产精品videosex极品| 日韩脚交footjobhd|