-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.go
159 lines (144 loc) · 3.04 KB
/
linkedlist.go
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
package datastruct
import (
"bytes"
"errors"
"fmt"
)
func NewLinkedList() *LinkedList {
ll := new(LinkedList)
return ll
}
func (ll *LinkedList) IndexOf(node *ListNode) (int, error) {
var index int
var tmpNode = ll.Head
for {
if index >= ll.length {
return -1, errors.New("No Node")
}
if node == tmpNode {
return index, nil
} else {
tmpNode = tmpNode.Next
index++
}
}
}
func (ll *LinkedList) Index(index int) (*ListNode, error) {
if index < 0 || index >= ll.length {
return nil, fmt.Errorf("out of index: %d is out of bounds, it should be between [0, %d)", index, ll.length)
}
if index == 0 {
return ll.Head, nil
}
tmpNode := ll.Head
for ; index > 0; index-- {
tmpNode = tmpNode.Next
}
return tmpNode, nil
}
func (ll *LinkedList) Insert(node *ListNode) {
node.Next = ll.Head
ll.Head = node
ll.length++
}
func (ll *LinkedList) InsertAt(node *ListNode, index int) error {
if index < 0 || index > ll.length {
return fmt.Errorf("index out of range: [0, %d], get %d", ll.length, index)
}
tmpNode := ll.Head
// if index == 0, it equals Insert
if index == 0 {
ll.Insert(node)
return nil
}
index--
for ; index > 0; index-- {
tmpNode = tmpNode.Next
}
// now tmpNode point to ll[index-1]
node.Next = tmpNode.Next
tmpNode.Next = node
ll.length++
return nil
}
//Pop will return the first node and delete it from the list
func (ll *LinkedList) Pop() (*ListNode, error) {
if ll.length == 0 {
return nil, errors.New("No Node")
}
tmpNode := ll.Head
ll.Head = ll.Head.Next
tmpNode.Next = nil
ll.length--
return tmpNode, nil
}
func (ll *LinkedList) String() string {
buf := bytes.NewBuffer([]byte{})
tmpNode := ll.Head
for i := 0; i < ll.length; i++ {
buf.WriteString(fmt.Sprintf("%v", tmpNode.Value))
if i != ll.length-1 {
buf.WriteString("->")
}
tmpNode = tmpNode.Next
}
return buf.String()
}
func (ll *LinkedList) DeleteByIndex(index int) error {
if index < 0 || index >= ll.length {
return fmt.Errorf("index out of range: [0, %d), get %d", ll.length, index)
}
if index == 0 {
ll.Head = ll.Head.Next
ll.length--
return nil
}
lastNode, _ := ll.Index(index - 1)
deleteNode := lastNode.Next
lastNode.Next = deleteNode.Next
deleteNode.Next = nil
ll.length--
return nil
}
func (ll *LinkedList) DeleteByNode(node *ListNode) error {
index, err := ll.IndexOf(node)
if err != nil {
return err
}
err = ll.DeleteByIndex(index)
if err != nil {
return err
}
return nil
}
func (ll *LinkedList) Modify(index int, value interface{}) error {
node, err := ll.Index(index)
if err != nil {
return err
}
node.Value = value
return nil
}
func (ll *LinkedList) GetFirst(value interface{}) (int, *ListNode) {
tmpNode := ll.Head
for i := 0; i < ll.length; i++ {
if tmpNode.Value == value {
return i, tmpNode
}
tmpNode = tmpNode.Next
}
return -1, nil
}
func (ll *LinkedList) GetLength() int {
return ll.length
}
func (ll *LinkedList) Clear() {
// use simple method
ll.Head = nil
ll.length = 0
}
func NewListNode(value interface{}) *ListNode {
ln := new(ListNode)
ln.Value = value
return ln
}