-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinked-list.go
204 lines (162 loc) · 3.34 KB
/
linked-list.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
)
func UseLinkedList() {
linkedList := &LinkedList{}
linkedList.Add(1)
linkedList.Add(2)
linkedList.Add(3)
linkedList.Add(4)
linkedList.PrintNodes()
if index, removed := linkedList.Remove(3); removed {
fmt.Println(fmt.Sprintf("Removed index %d", index))
linkedList.PrintNodes()
}
if index, exist := linkedList.Contains(2); exist {
fmt.Println(fmt.Sprintf("Exist index %d", index))
}
linkedList.RemoveHead()
linkedList.PrintNodes()
linkedList.RemoveTail()
linkedList.PrintNodes()
linkedList.Add(3)
linkedList.Add(4)
linkedList.PreAdd(1)
linkedList.PrintNodes()
fmt.Println(linkedList.Size())
}
type LinkedList struct {
count int
head *Node
tail *Node
}
type Node struct {
value interface{}
next *Node
}
func (l *LinkedList) Add(value interface{}) {
newNode := &Node{
value: value,
next: nil,
}
if l.head == nil {
l.head = newNode
l.tail = newNode
} else {
l.tail.next = newNode
l.tail = newNode
}
l.count++
}
func (l *LinkedList) Remove(element interface{}) (int, bool) {
current := l.head
var previous *Node
index := 0
for current != nil {
// find element
if current.value == element {
// is not first node
if previous != nil {
// before: Head -> 1 -> 2 -> 3 --> null
// after: Head -> 1 -> 2 -------> null
previous.next = current.next
// set last node if current already is last
if current.next == nil {
l.tail = previous
}
} else {
// before: Head -> 3 -> 5
// after: Head ------> 5
// Head -> 3 -> null
// Head ------> null
l.head = current.next
// check is empty list
if l.head == nil {
l.tail = nil
}
}
l.count--
return index, true
}
index++
previous = current
current = current.next
}
return index, false
}
func (l *LinkedList) Contains(element interface{}) (int, bool) {
current := l.head
index := 0
for current != nil {
if current.value == element {
return index, true
}
current = current.next
index++
}
return index, false
}
func (l *LinkedList) Size() int {
return l.count
}
func (l *LinkedList) PreAdd(value interface{}) {
newNode := &Node{
value: value,
next: l.head,
}
l.head = newNode
if l.tail == nil {
l.tail = newNode
}
l.count++
}
func (l *LinkedList) RemoveHead() (bool, *Node) {
if l.head == nil {
return false, nil
}
tmpHead := l.head
if l.head.next != nil {
l.head = l.head.next
} else {
l.head = nil
l.head = nil
}
l.count--
return true, tmpHead
}
func (l *LinkedList) RemoveTail() (bool, *Node) {
tmpTail := l.tail
if l.head.next == nil {
// There is only one node in linked list.
l.head = nil
l.tail = nil
l.count--
return true, tmpTail
}
// If there are many nodes in linked list...
// Rewind to the last node and delete "next" link for the node before the last one.
currentNode := l.head
for currentNode.next != nil {
if currentNode.next.next == nil {
currentNode.next = nil
} else {
currentNode = currentNode.next
}
}
l.count--
l.tail = currentNode
return true, tmpTail
}
// help method
func (l *LinkedList) PrintNodes() {
currentNode := l.head
chain := ""
for currentNode.next != nil {
chain += fmt.Sprintf("| %v : next | --> ", currentNode.value)
currentNode = currentNode.next
}
// last node
chain += fmt.Sprintf("| %v : next |", currentNode.value)
fmt.Println(chain)
}