-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnode.go
62 lines (55 loc) · 1.07 KB
/
node.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
package ordered_map
import (
"fmt"
"bytes"
)
type node struct {
Prev *node
Next *node
Value interface{}
}
func newRootNode() *node {
root := &node{}
root.Prev = root
root.Next = root
return root
}
func newNode(prev *node, next *node, key interface{}) *node {
return &node{Prev: prev, Next: next, Value: key}
}
func (n *node) Add(value string) {
root := n
last := root.Prev
last.Next = newNode(last, n, value)
root.Prev = last.Next
}
func (n *node) String() string {
var buffer bytes.Buffer
if n.Value == "" {
// Need to sentinel
var curr *node
root := n
curr = root.Next
for curr != root {
buffer.WriteString(fmt.Sprintf("%s, ", curr.Value))
curr = curr.Next
}
} else {
// Else, print pointer value
buffer.WriteString(fmt.Sprintf("%p, ", &n))
}
return fmt.Sprintf("LinkList[%v]", buffer.String())
}
func (n *node) IterFunc() func() (string, bool) {
var curr *node
root := n
curr = root.Next
return func() (string, bool) {
for curr != root {
tmp := curr.Value.(string)
curr = curr.Next
return tmp, true
}
return "", false
}
}