1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| package linkedlist
import ( "fmt" "github.com/pkg/errors" )
type Node struct { next *Node data interface{} }
type LinkedList struct { head *Node length uint }
func NewNode(value interface{}) *Node { return &Node{nil, value} }
func NewLinkedList() *LinkedList{ return &LinkedList{NewNode(0), 0} }
func (self *Node) GetNext(node *Node) *Node { if nil == node { return nil }
return node.next }
func (self *Node) GetValue(node *Node) interface{} { if nil == node { return nil }
return node.data }
func (self *LinkedList) InsertBefore(node *Node, v interface{}) error { if nil == node { return errors.New("Try to insert before a empty node") }
newNode := NewNode(v) curr := self.head.next pre := self.head
for nil != curr { if curr == node { break }
pre = curr curr = curr.next }
if nil == curr { return errors.New("Can not find node to insert") }
pre.next = newNode newNode.next = curr
self.length++
return nil }
func (self *LinkedList) InsertAfter(node *Node, v interface{}) error { if nil == node { return errors.New("Try to insert after a empty node") }
curr := self.head
for nil != curr { if curr == node { break }
curr = curr.next }
if curr == nil { return errors.New("Can not find node to insert") }
newNode := NewNode(v) nextNode := curr.next curr.next = newNode newNode.next = nextNode
self.length++
return nil }
func (self *LinkedList) Prepend(v interface{}) error { return self.InsertAfter(self.head, v) }
func (self *LinkedList) Append(v interface{}) error { newNode := NewNode(v)
head := self.head
if nil == head.next {
head.next = newNode
} else {
curr := head
for nil != curr.next { curr = curr.next }
curr.next = newNode }
self.length++
return nil }
func (self *LinkedList) FindByIndex(index uint) (*Node, error) {
if index > self.length { return nil, errors.New("Find out of range") }
curr := self.head
for i := uint(0); i< index; i++ { curr = curr.next }
return curr, nil }
func (self *LinkedList) FindByData(data interface{}) (*Node, error) { curr := self.head
for nil != curr { curr = curr.next }
return curr, nil }
func (self *LinkedList) DelNode(node *Node) (bool, error) { if nil == node { return false, errors.New("Try to delete empty node") }
curr := self.head
for nil != curr {
if(curr.next == node) { curr.next = curr.next.next
return true, nil }
curr = curr.next }
return false, nil }
func (self *LinkedList) Print() { curr := self.head
var formatStr string
for nil != curr.next { curr = curr.next
formatStr += fmt.Sprintf("|%+v", curr.data) }
fmt.Println(formatStr) }
|