|
3 | 3 | * @Date: 2020-02-10 22:49:50
|
4 | 4 | * @LastEditTime : 2020-02-11 16:17:52
|
5 | 5 | * @LastEditors : 百里
|
6 |
| - * @Description: |
| 6 | + * @Description: 单链表操作. |
7 | 7 | * @FilePath: \leetcode\02.链表\01.单链表.go
|
8 | 8 | * @https://github.com/yezihack
|
9 | 9 | */
|
10 | 10 | package linked
|
11 | 11 |
|
| 12 | +import "fmt" |
| 13 | + |
12 | 14 | type SNode struct {
|
13 |
| - data int |
14 |
| - next *SNode //后继节点 |
| 15 | + data int //数据存储 |
| 16 | + next *SNode //后继指针 |
15 | 17 | }
|
16 | 18 |
|
17 | 19 | type ISingleLinked interface {
|
18 |
| - AddHead(data int) //插入头节点 |
19 |
| - Append(data int) //追加节点,即添加到尾部. |
| 20 | + AddHead(data int) *SNode //插入头节点 |
| 21 | + Append(data int) *SNode //追加节点,即添加到尾部. |
20 | 22 | RemoveHead() bool //删除头节点
|
21 | 23 | RemoveTail() bool //删除尾节点
|
22 | 24 | RemoveNode(node *SNode) bool //删除节点
|
23 | 25 | Print() string //打印链表
|
24 | 26 | }
|
| 27 | + |
| 28 | +//声明单链表结构体. |
| 29 | +type SLinked struct { |
| 30 | + count int //length node |
| 31 | + head *SNode //head node |
| 32 | + tail *SNode //tail node |
| 33 | +} |
| 34 | + |
| 35 | +//实例一下 |
| 36 | +func NewSLinked() *SLinked { |
| 37 | + return &SLinked{} |
| 38 | +} |
| 39 | + |
| 40 | +//添加头节点 |
| 41 | +func (s *SLinked) AddHead(data int) *SNode { |
| 42 | + node := &SNode{data: data} //创建一个新节点. |
| 43 | + if s.head == nil { //如果头指针为空的话. |
| 44 | + s.head = node //node指向头指针 |
| 45 | + s.tail = node //node也指向尾指针. 即头尾指针指向同一个节点. |
| 46 | + } else { |
| 47 | + //实现头插法. |
| 48 | + node.next = s.head //新的节点node后继指针指向头节点head |
| 49 | + s.head = node //将node设置为新的头指针head. |
| 50 | + } |
| 51 | + s.count++ |
| 52 | + return node |
| 53 | +} |
| 54 | + |
| 55 | +//追加操作. |
| 56 | +func (s *SLinked) Append(data int) *SNode { |
| 57 | + node := &SNode{data: data} //创建一个新节点. |
| 58 | + if s.tail == nil { //如果尾指针为空的话. |
| 59 | + s.head = node //node指向头指针 |
| 60 | + s.tail = node //node也指向尾指针. 即头尾指向同一个节点. |
| 61 | + } else { |
| 62 | + //实现尾插法 |
| 63 | + s.tail.next = node //尾指针tail的后继指针指向新节点node |
| 64 | + s.tail = node //将新节点node设置为新的尾指针tail |
| 65 | + } |
| 66 | + s.count++ |
| 67 | + return node |
| 68 | +} |
| 69 | + |
| 70 | +//移除头节点. |
| 71 | +func (s *SLinked) RemoveHead() bool { |
| 72 | + if s.count == 0 { //如果无节点则返回False |
| 73 | + return false |
| 74 | + } |
| 75 | + s.head = s.head.next //将头指针的后继指针设置为新的头指针 |
| 76 | + s.count-- |
| 77 | + return true |
| 78 | +} |
| 79 | + |
| 80 | +func (s *SLinked) RemoveTail() bool { |
| 81 | + if s.count == 0 { //链表长度为0的话.则返回False |
| 82 | + return false |
| 83 | + } else if s.count == 1 { //链表长度为1的话,设置为空 |
| 84 | + s.tail = nil |
| 85 | + s.head = nil |
| 86 | + } else { |
| 87 | + tailPre := s.head //定义一个指针 |
| 88 | + for i := 0; i < s.count-2; i++ { //循环count-2 |
| 89 | + tailPre = tailPre.next |
| 90 | + } |
| 91 | + tailPre.next = nil |
| 92 | + s.tail = tailPre |
| 93 | + } |
| 94 | + s.count-- |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +func (s *SLinked) RemoveNode(node *SNode) bool { |
| 99 | + if node == s.head { |
| 100 | + s.head = s.head.next |
| 101 | + } else if node == s.tail { |
| 102 | + tailPre := s.head |
| 103 | + for i := 0; i < s.count-2; i++ { |
| 104 | + tailPre = tailPre.next |
| 105 | + } |
| 106 | + tailPre.next = nil |
| 107 | + s.tail = tailPre |
| 108 | + } else { |
| 109 | + l := s.head |
| 110 | + var pre *SNode |
| 111 | + for i := 0; i < s.count-1; i++ { |
| 112 | + if l == node { |
| 113 | + break |
| 114 | + } |
| 115 | + pre = l |
| 116 | + l = l.next |
| 117 | + } |
| 118 | + if pre.next != nil { |
| 119 | + pre.next = pre.next.next |
| 120 | + } |
| 121 | + } |
| 122 | + return true |
| 123 | +} |
| 124 | + |
| 125 | +func (s *SLinked) Print() string { |
| 126 | + str := "" |
| 127 | + head := s.head |
| 128 | + for head != nil { |
| 129 | + str += fmt.Sprint(head.data) |
| 130 | + head = head.next |
| 131 | + if head != nil { |
| 132 | + str += "=>" |
| 133 | + } |
| 134 | + } |
| 135 | + return str |
| 136 | +} |
0 commit comments