forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly.go
226 lines (185 loc) · 4.07 KB
/
doubly.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package linkedlist
import "fmt"
// Doubly structure with just the Head Node
// We call it `Doubly` to make it easier to
// understand when calling this in peoples
// own local code to understand and experiment
// with this easily.
// For example, we can use gotools `go get` command to get
// this repository cloned inside the
// $GOPATH/src/github.com/TheAlgorithms/Go (you can do this
// manually as well) and use the import statement as follows:
//
// `import "github.com/TheAlgorithms/Go/structure/linkedlist"`
//
// and call linkedlist.Doubly to create a new doubly linked list.
type Doubly[T any] struct {
Head *Node[T]
}
// Init initializes double linked list
func (ll *Doubly[T]) Init() *Doubly[T] {
ll.Head = &Node[T]{}
ll.Head.Next = ll.Head
ll.Head.Prev = ll.Head
return ll
}
func NewDoubly[T any]() *Doubly[T] {
return new(Doubly[T]).Init()
}
// lazyInit lazily initializes a zero List value.
func (ll *Doubly[T]) lazyInit() {
if ll.Head.Next == nil {
ll.Init()
}
}
func (ll *Doubly[T]) insert(n, at *Node[T]) *Node[T] {
n.Prev = at
n.Next = at.Next
n.Prev.Next = n
n.Next.Prev = n
return n
}
func (ll *Doubly[T]) insertValue(val T, at *Node[T]) *Node[T] {
return ll.insert(NewNode(val), at)
}
// AddAtBeg Add a node to the beginning of the linkedlist
func (ll *Doubly[T]) AddAtBeg(val T) {
ll.lazyInit()
ll.insertValue(val, ll.Head)
}
// AddAtEnd Add a node at the end of the linkedlist
func (ll *Doubly[T]) AddAtEnd(val T) {
ll.lazyInit()
ll.insertValue(val, ll.Head.Prev)
}
func (ll *Doubly[T]) Remove(n *Node[T]) T {
n.Prev.Next = n.Next
n.Next.Prev = n.Prev
n.Next = nil
n.Prev = nil
return n.Val
}
// DelAtBeg Delete the node at the beginning of the linkedlist
func (ll *Doubly[T]) DelAtBeg() (T, bool) {
// no item
if ll.Head.Next == nil {
var r T
return r, false
}
n := ll.Head.Next
val := n.Val
ll.Remove(n)
return val, true
}
// DetAtEnd Delete a node at the end of the linkedlist
func (ll *Doubly[T]) DelAtEnd() (T, bool) {
// no item
if ll.Head.Prev == nil {
var r T
return r, false
}
n := ll.Head.Prev
val := n.Val
ll.Remove(n)
return val, true
}
// DelByPos deletes node at middle based on position in list
// and returns value. If empty or position of node is less than linked list length, returns false
func (ll *Doubly[T]) DelByPos(pos int) (T, bool) {
switch {
case ll.Head == nil:
var r T
return r, false
case pos-1 == 0:
return ll.DelAtBeg()
case pos-1 == ll.Count():
return ll.DelAtEnd()
case pos-1 > ll.Count():
var r T
return r, false
}
var prev *Node[T]
var val T
cur := ll.Head
count := 0
for count < pos-1 {
prev = cur
cur = cur.Next
count++
}
cur.Next.Prev = prev
val = cur.Val
prev.Next = cur.Next
return val, true
}
// Count Number of nodes in the linkedlist
func (ll *Doubly[T]) Count() int {
var ctr int = 0
if ll.Head.Next == nil {
return 0
}
for cur := ll.Head.Next; cur != ll.Head; cur = cur.Next {
ctr += 1
}
return ctr
}
// Reverse Reverse the order of the linkedlist
func (ll *Doubly[T]) Reverse() {
var Prev, Next *Node[T]
cur := ll.Head
for cur != nil {
Next = cur.Next
cur.Next = Prev
cur.Prev = Next
Prev = cur
cur = Next
}
ll.Head = Prev
}
// Display display the linked list
func (ll *Doubly[T]) Display() {
for cur := ll.Head.Next; cur != ll.Head; cur = cur.Next {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
// DisplayReverse Display the linkedlist in reverse order
func (ll *Doubly[T]) DisplayReverse() {
if ll.Head == nil {
return
}
var cur *Node[T]
for cur = ll.Head.Prev; cur != ll.Head; cur = cur.Prev {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
func (ll *Doubly[T]) Front() *Node[T] {
if ll.Count() == 0 {
return nil
}
return ll.Head.Next
}
func (ll *Doubly[T]) Back() *Node[T] {
if ll.Count() == 0 {
return nil
}
return ll.Head.Prev
}
func (ll *Doubly[T]) MoveToBack(n *Node[T]) {
if ll.Head.Prev == n {
return
}
ll.move(n, ll.Head.Prev)
}
func (ll *Doubly[T]) move(n, at *Node[T]) {
if n == at {
return
}
n.Prev.Next = n.Next
n.Next.Prev = n.Prev
n.Prev = at
n.Next = at.Next
n.Prev.Next = n
n.Next.Prev = n
}