-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
77 lines (66 loc) · 1.31 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package linked
type Node struct {
next, prev *Node
// The List to which this Node belongs.
list *List
// The value stored with this Node.
Value interface{}
}
func (n *Node) Attach(node *Node) *Node {
if n == node {
return n
}
if node.prev != nil {
node.prev.next = node.next
}
if node.next != nil {
node.next.prev = node.prev
}
if n.next != nil {
n.next.prev = node
}
node.next, n.next = n.next, node
node.prev = n
if node.list != n.list {
if node.list != nil {
node.list.length--
}
if n.list != nil {
n.list.length++
}
node.list = n.list
}
return node
}
func (n *Node) Detach() *Node {
if n.prev != nil {
n.prev.next = n.next
}
if n.next != nil {
n.next.prev = n.prev
}
n.next = nil
n.prev = nil
if n.list != nil {
n.list.length--
n.list = nil
}
return n
}
// List returns the pointer to the List that the Node belongs to.
func (n *Node) List() *List { return n.list }
// Next returns the pointer to the next Node or nil.
func (n *Node) Next() *Node {
if n.list != nil && n.next == &n.list.root {
return nil
}
return n.next
}
// Prev returns the pointer to the previous Node or nil.
func (n *Node) Prev() *Node {
if n.list != nil && n.prev == &n.list.root {
return nil
}
return n.prev
}
func NewNode(value interface{}) *Node { return &Node{Value: value} }