|
1 | 1 | package main
|
2 | 2 |
|
3 |
| -import ( |
4 |
| - "reflect" |
5 |
| - "testing" |
6 |
| -) |
| 3 | +import "fmt" |
7 | 4 |
|
8 | 5 | type Node struct {
|
9 | 6 | Value int
|
10 | 7 | Next *Node
|
11 |
| - Prev *Node |
| 8 | + Prev *Node |
12 | 9 | }
|
13 | 10 |
|
14 | 11 | type DoublyLinkedList struct {
|
15 | 12 | Head *Node
|
16 |
| - Tail *Node |
| 13 | + Tail *Node |
17 | 14 | }
|
18 | 15 |
|
19 | 16 | func (list *DoublyLinkedList) AddLeft(value int) {
|
20 | 17 | node := Node{Value: value}
|
21 | 18 |
|
22 |
| - if list.Head == nil { |
23 |
| - list.Head = &node |
24 |
| - list.Tail = &node |
25 |
| - return |
26 |
| - } |
| 19 | + if list.Head == nil { |
| 20 | + list.Head = &node |
| 21 | + list.Tail = &node |
| 22 | + return |
| 23 | + } |
27 | 24 |
|
28 |
| - node.Next = list.Head |
29 |
| - list.Head.Prev = &node |
30 |
| - list.Head = &node |
| 25 | + node.Next = list.Head |
| 26 | + list.Head.Prev = &node |
| 27 | + list.Head = &node |
31 | 28 | }
|
32 | 29 |
|
33 | 30 | func (list *DoublyLinkedList) AddRight(value int) {
|
34 |
| - node := Node{Value: value} |
| 31 | + node := Node{Value: value} |
35 | 32 |
|
36 |
| - if list.Tail == nil { |
37 |
| - list.Tail = &node |
38 |
| - list.Head = &node |
39 |
| - return |
40 |
| - } |
| 33 | + if list.Tail == nil { |
| 34 | + list.Tail = &node |
| 35 | + list.Head = &node |
| 36 | + return |
| 37 | + } |
41 | 38 |
|
42 |
| - node.Prev = list.Tail |
43 |
| - list.Tail.Next = &node |
44 |
| - list.Tail = &node |
| 39 | + node.Prev = list.Tail |
| 40 | + list.Tail.Next = &node |
| 41 | + list.Tail = &node |
45 | 42 | }
|
46 | 43 |
|
47 | 44 | func (list *DoublyLinkedList) RemoveLeft() {
|
48 |
| - if list.Head == nil { |
49 |
| - return |
50 |
| - } |
| 45 | + if list.Head == nil { |
| 46 | + return |
| 47 | + } |
51 | 48 |
|
52 |
| - list.Head = list.Head.Next |
| 49 | + list.Head = list.Head.Next |
53 | 50 |
|
54 |
| - if list.Head != nil { |
55 |
| - list.Head.Prev = nil |
56 |
| - } else { |
57 |
| - list.Tail = nil |
58 |
| - } |
| 51 | + if list.Head != nil { |
| 52 | + list.Head.Prev = nil |
| 53 | + } else { |
| 54 | + list.Tail = nil |
| 55 | + } |
59 | 56 | }
|
60 | 57 |
|
61 | 58 | func (list *DoublyLinkedList) RemoveRight() {
|
62 |
| - if list.Tail == nil { |
63 |
| - return |
64 |
| - } |
| 59 | + if list.Tail == nil { |
| 60 | + return |
| 61 | + } |
65 | 62 |
|
66 |
| - list.Tail = list.Tail.Prev |
| 63 | + list.Tail = list.Tail.Prev |
67 | 64 |
|
68 |
| - if list.Tail != nil { |
69 |
| - list.Tail.Next = nil |
70 |
| - } else { |
71 |
| - list.Head = nil |
72 |
| - } |
| 65 | + if list.Tail != nil { |
| 66 | + list.Tail.Next = nil |
| 67 | + } else { |
| 68 | + list.Head = nil |
| 69 | + } |
73 | 70 | }
|
74 | 71 |
|
75 | 72 | func (list *DoublyLinkedList) FindAt(idx int) *Node {
|
76 |
| - pointer := list.Head |
| 73 | + pointer := list.Head |
77 | 74 |
|
78 |
| - for i := 0; i < idx; i++ { |
79 |
| - pointer = pointer.Next |
80 |
| - if pointer == nil { |
81 |
| - return nil |
82 |
| - } |
83 |
| - } |
| 75 | + for i := 0; i < idx; i++ { |
| 76 | + pointer = pointer.Next |
| 77 | + if pointer == nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + } |
84 | 81 |
|
85 |
| - return pointer |
| 82 | + return pointer |
86 | 83 | }
|
87 | 84 |
|
88 | 85 | func (list *DoublyLinkedList) AddAt(idx, value int) {
|
89 |
| - if idx == 0 { |
90 |
| - list.AddLeft(value) |
91 |
| - return |
92 |
| - } |
| 86 | + if idx == 0 { |
| 87 | + list.AddLeft(value) |
| 88 | + return |
| 89 | + } |
93 | 90 |
|
94 |
| - foundNode := list.FindAt(idx) |
95 |
| - if foundNode == nil { |
96 |
| - return |
97 |
| - } |
| 91 | + foundNode := list.FindAt(idx) |
| 92 | + if foundNode == nil { |
| 93 | + return |
| 94 | + } |
98 | 95 |
|
99 |
| - prevNode := foundNode.Prev |
100 |
| - if prevNode == nil { |
101 |
| - return |
102 |
| - } |
| 96 | + prevNode := foundNode.Prev |
| 97 | + if prevNode == nil { |
| 98 | + return |
| 99 | + } |
103 | 100 |
|
104 |
| - newNode := Node{Value: value} |
| 101 | + newNode := Node{Value: value} |
105 | 102 |
|
106 |
| - newNode.Prev = prevNode |
107 |
| - newNode.Next = foundNode |
| 103 | + newNode.Prev = prevNode |
| 104 | + newNode.Next = foundNode |
108 | 105 |
|
109 |
| - prevNode.Next = &newNode |
110 |
| - foundNode.Prev = &newNode |
| 106 | + prevNode.Next = &newNode |
| 107 | + foundNode.Prev = &newNode |
111 | 108 | }
|
112 | 109 |
|
113 | 110 | func (list *DoublyLinkedList) RemoveAt(idx int) {
|
114 |
| - if idx == 0 { |
115 |
| - list.RemoveLeft() |
116 |
| - return |
117 |
| - } |
| 111 | + if idx == 0 { |
| 112 | + list.RemoveLeft() |
| 113 | + return |
| 114 | + } |
118 | 115 |
|
119 |
| - foundNode := list.FindAt(idx) |
120 |
| - if foundNode == nil { |
121 |
| - return |
122 |
| - } |
| 116 | + foundNode := list.FindAt(idx) |
| 117 | + if foundNode == nil { |
| 118 | + return |
| 119 | + } |
123 | 120 |
|
124 |
| - prevNode := foundNode.Prev |
125 |
| - if prevNode == nil { |
126 |
| - return |
127 |
| - } |
| 121 | + prevNode := foundNode.Prev |
| 122 | + if prevNode == nil { |
| 123 | + return |
| 124 | + } |
128 | 125 |
|
129 |
| - prevNode.Next = foundNode.Next |
| 126 | + prevNode.Next = foundNode.Next |
130 | 127 |
|
131 |
| - if foundNode.Next == nil { |
132 |
| - list.Tail = prevNode |
133 |
| - return |
134 |
| - } |
| 128 | + if foundNode.Next == nil { |
| 129 | + list.Tail = prevNode |
| 130 | + return |
| 131 | + } |
135 | 132 |
|
136 |
| - foundNode.Next.Prev = prevNode |
| 133 | + foundNode.Next.Prev = prevNode |
137 | 134 | }
|
138 | 135 |
|
139 | 136 | func (list *DoublyLinkedList) ToSlice() []int {
|
140 |
| - var slice []int |
| 137 | + var slice []int |
141 | 138 |
|
142 |
| - for pointer := list.Head; pointer != nil; pointer = pointer.Next { |
143 |
| - slice = append(slice, pointer.Value) |
144 |
| - } |
| 139 | + for pointer := list.Head; pointer != nil; pointer = pointer.Next { |
| 140 | + slice = append(slice, pointer.Value) |
| 141 | + } |
145 | 142 |
|
146 |
| - return slice |
| 143 | + return slice |
147 | 144 | }
|
148 | 145 |
|
149 |
| -func TestDoublyLinkedList(t *testing.T) { |
| 146 | +func main() { |
150 | 147 | list := DoublyLinkedList{}
|
151 | 148 |
|
152 |
| - // test AddLeft |
| 149 | + fmt.Println("AddLeft(1, 2)") |
153 | 150 | list.AddLeft(1)
|
154 | 151 | list.AddLeft(2)
|
155 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{2, 1}) { |
156 |
| - t.Errorf("AddLeft failed, expected %v, got %v", []int{2, 1}, list.ToSlice()) |
157 |
| - } |
158 | 152 |
|
159 |
| - // test AddRight |
| 153 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 154 | + |
| 155 | + fmt.Println("AddRight(3, 4)") |
160 | 156 | list.AddRight(3)
|
161 | 157 | list.AddRight(4)
|
162 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{2, 1, 3, 4}) { |
163 |
| - t.Errorf("AddRight failed, expected %v, got %v", []int{2, 1, 3, 4}, list.ToSlice()) |
164 |
| - } |
165 | 158 |
|
166 |
| - //// test RemoveLeft |
| 159 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 160 | + |
| 161 | + fmt.Println("RemoveLeft()") |
167 | 162 | list.RemoveLeft()
|
168 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{1, 3, 4}) { |
169 |
| - t.Errorf("RemoveLeft failed, expected %v, got %v", []int{1, 3, 4}, list.ToSlice()) |
170 |
| - } |
171 | 163 |
|
172 |
| - // test RemoveRight |
| 164 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 165 | + |
| 166 | + fmt.Println("RemoveRight()") |
173 | 167 | list.RemoveRight()
|
174 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{1, 3}) { |
175 |
| - t.Errorf("RemoveRight failed, expected %v, got %v", []int{1, 3}, list.ToSlice()) |
176 |
| - } |
177 | 168 |
|
178 |
| - // test AddAt |
| 169 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 170 | + |
| 171 | + fmt.Println("AddAt(1, 5)") |
179 | 172 | list.AddAt(1, 5)
|
180 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{1, 5, 3}) { |
181 |
| - t.Errorf("AddAt failed, expected %v, got %v", []int{1, 5, 3}, list.ToSlice()) |
182 |
| - } |
183 | 173 |
|
184 |
| - // test RemoveAt |
| 174 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 175 | + |
| 176 | + fmt.Println("RemoveAt(2)") |
185 | 177 | list.RemoveAt(2)
|
186 |
| - if !reflect.DeepEqual(list.ToSlice(), []int{1, 5}) { |
187 |
| - t.Errorf("RemoveAt failed, expected %v, got %v", []int{1, 5}, list.ToSlice()) |
188 |
| - } |
189 | 178 |
|
190 |
| - // test FindAt |
| 179 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 180 | + |
| 181 | + fmt.Println("FindAt(0)") |
191 | 182 | node := list.FindAt(0)
|
192 |
| - if node == nil || node.Value != 1 { |
193 |
| - t.Errorf("FindAt failed, expected %v, got %v", 1, node.Value) |
194 |
| - } |
| 183 | + fmt.Println("node.Value: ", node.Value) |
195 | 184 | }
|
0 commit comments