-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneWayLinkedList.go
197 lines (174 loc) · 3.58 KB
/
OneWayLinkedList.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
/*
* @Name: One Way Linked List Go
* @Author: Max Base
* @Date: 2022-11-14
* @Repository: https://github.com/basemax/OneWayLinkedListGo
*/
package main
import "fmt"
type Node struct {
Value interface{}
Next *Node
}
type List struct {
Head *Node
}
// Create, O(1)
func (list *List) Create(value interface{}) {
list.Head = &Node{Value: value}
}
// InsertBegin, O(1)
func (list *List) InsertBegin(value interface{}) {
node := &Node{Value: value}
node.Next = list.Head
list.Head = node
}
// InsertEnd, O(n)
func (list *List) InsertEnd(value interface{}) {
node := list.Head
for node.Next != nil {
node = node.Next
}
node.Next = &Node{Value: value}
}
// InsertAfter, O(n)
func (list *List) InsertAfter(value interface{}, after interface{}) {
node := list.Head
for node != nil {
if node.Value == after {
newNode := &Node{Value: value}
newNode.Next = node.Next
node.Next = newNode
return
}
node = node.Next
}
}
// InsertBefore, O(n)
func (list *List) InsertBefore(value interface{}, before interface{}) {
node := list.Head
for node != nil {
if node.Next.Value == before {
newNode := &Node{Value: value}
newNode.Next = node.Next
node.Next = newNode
return
}
node = node.Next
}
}
// Delete, O(1)
// func (list *List) Delete() {
// if list.Head != nil {
// list.Head = list.Head.Next
// }
// }
// DeleteBegin, O(1)
func (list *List) DeleteBegin() {
if list.Head != nil {
list.Head = list.Head.Next
}
}
// DeleteEnd, O(n)
func (list *List) DeleteEnd() {
node := list.Head
for node.Next.Next != nil {
node = node.Next
}
node.Next = nil
}
// DeleteValue, O(n)
func (list *List) DeleteValue(value interface{}) {
node := list.Head
for node != nil {
if node.Next.Value == value {
if node.Next.Next != nil {
node.Next = node.Next.Next
} else {
node.Next = nil
}
return
}
node = node.Next
}
}
// DeleteAfterValue, O(n)
func (list *List) DeleteAfterValue(value interface{}) {
node := list.Head
for node != nil {
if node.Value == value {
node.Next = node.Next.Next
return
}
node = node.Next
}
}
// DeleteBeforeValue, O(n)
func (list *List) DeleteBeforeValue(value interface{}) {
node := list.Head
for node != nil {
if node.Next.Next.Value == value {
node.Next = node.Next.Next
return
}
node = node.Next
}
}
// DeleteIndex, O(n)
func (list *List) DeleteIndex(index int) {
if index < 0 {
return
}
node := list.Head
for i := 0; i < index; i++ {
node = node.Next
}
node.Next = node.Next.Next
}
// DeleteAfterIndex, O(n)
func (list *List) DeleteAfterIndex(index int) {
// list.DeleteIndex(index + 1)
node := list.Head
for i := 0; i < index; i++ {
node = node.Next
}
node.Next = node.Next.Next
}
// DeleteBeforeIndex, O(n)
func (list *List) DeleteBeforeIndex(index int) {
// list.DeleteIndex(index - 1)
node := list.Head
for i := 0; i < index-1; i++ {
node = node.Next
}
node.Next = node.Next.Next
}
// Search, O(n)
func (list *List) Search(value interface{}) bool {
node := list.Head
for node != nil {
if node.Value == value {
return true
}
node = node.Next
}
return false
}
// Print, O(n)
func (list *List) Print() {
node := list.Head
for node != nil {
fmt.Println(node.Value)
node = node.Next
}
}
// ToString, O(n)
func (list *List) ToString() string {
node := list.Head
result := ""
for node != nil {
result += fmt.Sprintf("%v", node.Value)
node = node.Next
}
return result
}