双向链表

双链的删除,查找,增加都优于单链表,灵活性也更高,为什么市面上用的大部分还是单链?是由于双链的每个节点都多了一个指向上一个节点的指针,在存储中一个指针在32位机上占4b,64位机占8b,就算这个原因,导致实际中单链才是比较合适的
image.png

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. )
  6. type Node struct {
  7. Pre *Node
  8. Value int
  9. Next *Node
  10. }
  11. type List struct{
  12. Size uint
  13. RW *sync.RWMutex
  14. Head *Node // 首节点
  15. Tail *Node // 尾节点
  16. }
  17. //初始化
  18. func NewList() *List{
  19. return &List{
  20. Size:0,
  21. RW:new(sync.RWMutex),
  22. Head:nil,
  23. Tail:nil,
  24. }
  25. }
  26. // 向尾部添加元素
  27. func (this *List) Append(value int){
  28. node := &Node{Value:value}
  29. this.RW.RLock()
  30. defer this.RW.RUnlock()
  31. if this.Size == 0{ // 当前链表是空的
  32. node.Next,node.Pre = nil,nil
  33. this.Head,this.Tail = node,node //头尾都是node
  34. }else {
  35. node.Next,node.Pre = nil,this.Tail
  36. this.Tail.Next = node // 尾节点Next指向新增得节点
  37. this.Tail = node //更新list尾节点
  38. }
  39. this.Size++ // 长度增加
  40. }
  41. // 向头部添加元素
  42. func (this *List) Add(value int){
  43. node := &Node{Value:value}
  44. this.RW.RLock()
  45. defer this.RW.RUnlock()
  46. if this.Size == 0{ // 当前链表是空的
  47. node.Next,node.Pre = nil,nil
  48. this.Head,this.Tail = node,node //头尾都是node
  49. }else {
  50. node.Next,node.Pre = this.Head,nil
  51. this.Head.Pre = node //头节点得pre指向新增得节点
  52. this.Head = node // 更新list头节点
  53. }
  54. this.Size++ // 长度增加
  55. }
  56. // 从左到右遍历插入
  57. func (this *List) InsertForward(index,value int){
  58. this.RW.RLock()
  59. defer this.RW.RUnlock()
  60. current := this.Head //index-1处得node
  61. for index > 1{
  62. current = current.Next
  63. index--
  64. }
  65. node := &Node{Value:value}
  66. //新node得pre指向index-1处得node
  67. //新node得next指向index处得node
  68. node.Pre,node.Next = current,current.Next
  69. //index处得node得pre更新为新增得node
  70. //index-1处得node得next更新为新增得node
  71. current.Next.Pre,current.Next = node,node
  72. this.Size++ // 长度增加
  73. }
  74. // 从右到左遍历插入
  75. func (this *List) InsertBackwards(index,value int){
  76. this.RW.RLock()
  77. defer this.RW.RUnlock()
  78. _index := this.Size-uint(index) //逆向
  79. current := this.Tail //index-1处得node
  80. for _index > 1{
  81. current = current.Pre
  82. _index--
  83. }
  84. node := &Node{Value:value}
  85. node.Pre,node.Next = current.Pre,current
  86. current.Pre,current.Pre.Next = node,node
  87. this.Size++ // 长度增加
  88. }
  89. //插入
  90. func (this *List) Insert(index,value int){
  91. if index <= 0 {
  92. this.Add(value)
  93. }else if uint(index) >= this.Size-1{
  94. this.Append(value)
  95. }else {
  96. // 如果插入得index得值在中间得左端,则向前遍历进行插入
  97. if this.Size/2>uint(index){
  98. this.InsertForward(index,value)
  99. }else {
  100. this.InsertBackwards(index,value)
  101. }
  102. }
  103. }
  104. // 删除与查找 ---应同插入一致,需要考虑到是从头部还剩从尾部开始遍历
  105. //遍历
  106. func(this *List) ForEach(opt ...string){
  107. //从左到右
  108. if opt == nil{
  109. current := this.Head
  110. for {
  111. fmt.Println(current.Value)
  112. if current.Next == nil{
  113. break
  114. }
  115. current = current.Next
  116. }
  117. }else {
  118. current := this.Tail
  119. for {
  120. fmt.Println(current.Value)
  121. if current.Pre == nil{
  122. break
  123. }
  124. current = current.Pre
  125. }
  126. }
  127. }
  128. func main() {
  129. list := NewList()
  130. list.Append(1)
  131. list.Append(2)
  132. list.Add(0)
  133. list.Append(3)
  134. list.Insert(1,4)
  135. list.Insert(3,5)
  136. list.ForEach("")
  137. }