-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyoo.go
197 lines (173 loc) · 3.83 KB
/
singlyoo.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
package singlyoo
import (
"errors"
"reflect"
)
type LinkedList[T any] struct {
head *Node[T]
}
// Node represents a singly linked list node
type Node[T any] struct {
data T
next *Node[T]
}
// NewList creates an empty linked list
func NewList[T any]() *LinkedList[T] {
return &LinkedList[T]{}
}
// NewListOf creates a new linked list form the parameters
func NewListOf[T any](ds ...T) *LinkedList[T] {
l := newList(ds...)
return &LinkedList[T]{head: l}
}
// Push inserts a node at the beginning of the list
func (list *LinkedList[T]) Push(data T) {
list.head = newNode(data, list.head)
}
// InsertAfter inserts a node after a given node
func (list *LinkedList[T]) InsertAfter(node *Node[T], data T) {
n := newNode(data, node.next)
node.next = n
}
// Append inserts a node at the end of the list
func (list *LinkedList[T]) Append(data T) {
list.InsertAfter(list.Last(), data)
}
func (list *LinkedList[T]) Last() *Node[T] {
h := list.head
for h.next != nil {
h = h.next
}
return h
}
// newNode creates a new singly linked list
func newNode[T any](data T, next *Node[T]) *Node[T] {
return &Node[T]{data, next}
}
// newList is a variadic constructor for singly linked list
func newList[T any](ds ...T) *Node[T] {
if len(ds) == 0 {
return nil
}
return newNode(ds[0], newList((ds[1:])...))
}
// Pop removes first node from the list and returns it
func (list *LinkedList[T]) Pop() *Node[T] {
n := list.head
list.head = n.next
return n
}
// Delete removes first matching element form the list
func (list *LinkedList[T]) Delete(data T) {
if reflect.DeepEqual(list.head.data, data) {
list.head = list.head.next
return
}
prev := list.head
next := list.head.next
for !reflect.DeepEqual(next.data, data) && next != nil {
prev = next
next = next.next
}
if next != nil {
prev.next = next.next
}
}
// DeleteWhile removes nodes from the head while matches a condition
func (list *LinkedList[T]) DeleteWhile(selector func(*Node[T]) bool) {
h := list.head
for selector(h) {
h = h.next
}
list.head = h
}
// DeleteWhen removes all those nodes when matches a condition
func (list *LinkedList[T]) DeleteWhen(selector func(*Node[T]) bool) {
prev := list.head
next := prev.next
for next != nil {
if selector(next) {
prev.next = next.next
} else {
prev = next
}
next = prev.next
}
if selector(list.head) {
list.head = list.head.next
}
}
// DeleteAt takes an index and remove item at that index
func (list *LinkedList[T]) DeleteAt(index int) {
if index == 0 {
list.head = list.head.next
return
}
cnt := 1
prev := list.head
next := prev.next
for cnt < index {
prev = next
next = prev.next
cnt++
}
prev.next = next.next
}
// Item takes an index at returns node at that index
func (list *LinkedList[T]) Item(index int) (T, error) {
var t T
if index < 0 {
return t, errors.New("index out of range")
}
n := list.head
cnt := 0
for cnt < index {
cnt++
if n == nil {
return t, errors.New("index out of range")
}
n = n.next
}
return n.data, nil
}
// 1, 2, 3, 4
// 1-> nil
// ReverseIter reverses the elements of a list
func (list *LinkedList[T]) ReverseIter() {
var prev *Node[T]
curr := list.head
for curr != nil {
temp := curr.next
curr.next = prev
prev = curr
curr = temp
}
list.head = prev
}
// 1,2,3,4
func (list *LinkedList[T]) ReverseIterFunc() {
var reverse func(prev, curr *Node[T]) *Node[T]
reverse = func(prev, curr *Node[T]) *Node[T] {
if curr == nil {
return prev
}
n := curr.next
curr.next = prev
return reverse(curr, n)
}
list.head = reverse(nil, list.head)
}
// 1,2,3,4,5
func (list *LinkedList[T]) ReverseRecurr() {
var reverse func(head *Node[T]) *Node[T]
reverse = func(head *Node[T]) *Node[T] {
newHead := head
if head.next != nil {
newHead = reverse(head.next)
head.next.next = head
}
head.next = nil
return newHead
}
list.head = reverse(list.head)
}