-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdll.go
52 lines (47 loc) · 863 Bytes
/
dll.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
package trie
// TODO: tests
type doublyLinkedList struct {
head, tail *dllNode
}
type dllNode struct {
trieNode *Node
next, prev *dllNode
}
func newDLLNode(trieNode *Node) *dllNode {
return &dllNode{trieNode: trieNode}
}
func (dll *doublyLinkedList) append(node *dllNode) {
if dll.head == nil {
dll.head = node
dll.tail = node
return
}
dll.tail.next = node
node.prev = dll.tail
dll.tail = node
}
func (dll *doublyLinkedList) pop(node *dllNode) {
if node == dll.head && node == dll.tail {
dll.head = nil
dll.tail = nil
return
}
if node == dll.head {
dll.head = node.next
dll.head.prev = nil
node.next = nil
return
}
if node == dll.tail {
dll.tail = node.prev
dll.tail.next = nil
node.prev = nil
return
}
prev := node.prev
next := node.next
prev.next = next
next.prev = prev
node.next = nil
node.prev = nil
}