-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedList.go
82 lines (70 loc) · 1.37 KB
/
DoubleLinkedList.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
package dataStruct
import "fmt"
type Node struct {
Next *Node
Prev *Node
Val int
}
type DoubleLinkedList struct {
Root *Node
Tail *Node
}
func (l *DoubleLinkedList) AddNode(Val int) {
if l.Root == nil {
l.Root = &Node{Val: Val}
l.Tail = l.Root
return
}
l.Tail.Next = &Node{Val: Val}
Prev := l.Tail
l.Tail = l.Tail.Next
l.Tail.Prev = Prev
}
func (l *DoubleLinkedList) Back() int {
if l.Tail != nil {
return l.Tail.Val
}
return 0
}
func (l *DoubleLinkedList) PopBack() {
if l.Tail == nil {
return
}
l.RemoveNode(l.Tail)
}
func (l *DoubleLinkedList) RemoveNode(node *Node) {
if node == l.Root {
l.Root = l.Root.Next
l.Root.Prev = nil
node.Next = nil
return
}
Prev := node.Prev
// Tail을 지우고자 한다면, 그 전 노드를 찾아야한다. 왜냐하면 전 노드의 Next에 nil을 넣어야 해당 노드가 Tail이 되므로.
if node == l.Tail {
Prev.Next = nil
l.Tail.Prev = nil
l.Tail = Prev
} else {
node.Prev = nil
Prev.Next = Prev.Next.Next
Prev.Next.Prev = Prev
}
node.Next = nil
}
func (l *DoubleLinkedList) PrintNodes() {
node := l.Root
for node.Next != nil {
fmt.Printf("%d ->", node.Val)
node = node.Next
}
fmt.Printf("%d\n", node.Val)
}
func (l *DoubleLinkedList) PrintReverse() {
node := l.Tail
for node.Prev != nil {
fmt.Printf("%d ->", node.Val)
node = node.Prev
}
fmt.Printf("%d\n", node.Val)
}