-
Notifications
You must be signed in to change notification settings - Fork 20
/
DoublyCircularLinkedList.go
239 lines (208 loc) · 5.08 KB
/
DoublyCircularLinkedList.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
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Doubly Circular Linked List Interface
*/
package main
import "fmt"
type Any interface{}
type Node struct {
Data Any
LeftLink *Node
RightLink *Node
}
// Return data: O(1)
func (n *Node) GetData() Any {
return n.Data
}
// Set data: O(1)
func (n *Node) SetData(data Any) {
n.Data = data
}
// Return left node: O(1)
func (n *Node) GetLeft() *Node {
return n.LeftLink
}
// Set left node: O(1)
func (n *Node) SetLeft(left *Node) {
n.LeftLink = left
}
// Test whether node has left node: O(1)
func (n *Node) HasLeft() bool {
if n.LeftLink == nil { return false }
return true
}
// Return right node: O(1)
func (n *Node) GetRight() *Node {
return n.RightLink
}
// Set right node: O(1)
func (n *Node) SetRight(right *Node) {
n.RightLink = right
}
// Test whether node has right node: O(1)
func (n *Node) HasRight() bool {
if n.RightLink == nil { return false }
return true
}
type DCLL struct {
Head *Node
count int
}
// Create a new Doubly Circular Linked List: O(1)
func NewDCLL() *DCLL {
return &DCLL{}
}
// Return size: O(1)
func (d *DCLL) Count() int {
return d.count
}
// Insert as head, set old head as head's left: O(1)
func (d *DCLL) InsertLeft(newnode *Node) {
if d.Head == nil {
d.Head = newnode
newnode.SetLeft(newnode)
newnode.SetRight(newnode)
d.count++
return
}
right := d.Head.GetRight()
newnode.SetRight(right)
right.SetLeft(newnode)
newnode.SetLeft(d.Head)
d.Head.SetRight(newnode)
d.Head = newnode
d.count++
}
// Insert as head, set old head as head's right: O(1)
func (d *DCLL) InsertRight(newnode *Node) {
if d.Head == nil {
d.Head = newnode
newnode.SetLeft(newnode)
newnode.SetRight(newnode)
d.count++
return
}
left := d.Head.GetLeft()
newnode.SetLeft(left)
left.SetRight(newnode)
newnode.SetRight(d.Head)
d.Head.SetLeft(newnode)
d.Head = newnode
d.count++
}
// Delete head, set head's left as new head: O(1)
func (d *DCLL) DeleteLeft() {
if d.Head != nil {
if d.count == 1 {
d.Head = nil
d.count--
return
}
left, right := d.Head.GetLeft(), d.Head.GetRight()
left.SetRight(right)
right.SetLeft(left)
d.Head = left
d.count--
}
}
// Delete head, set head's right as new head: O(1)
func (d *DCLL) DeleteRight() {
if d.Head != nil {
if d.count == 1 {
d.Head = nil
d.count--
return
}
left, right := d.Head.GetLeft(), d.Head.GetRight()
left.SetRight(right)
right.SetLeft(left)
d.Head = right
d.count--
}
}
// Set head's right as new head: O(1)
func (d *DCLL) RotateLeft() {
if d.Head != nil {
d.Head = d.Head.GetRight()
}
}
// Set head's left as new head: O(1)
func (d *DCLL) RotateRight() {
if d.Head != nil {
d.Head = d.Head.GetLeft()
}
}
// Print to left: O(N)
func (d *DCLL) PrintToLeft() {
if d.Head == nil {
fmt.Println("| Empty |")
return
}
current := d.Head
fmt.Print("|")
for {
fmt.Printf(" %#v |", current.Data)
current = current.GetLeft()
if current == d.Head { break }
}
fmt.Println()
}
// Print to right: O(N)
func (d *DCLL) PrintToRight() {
if d.Head == nil {
fmt.Println("| Empty |")
return
}
current := d.Head
fmt.Print("|")
for {
fmt.Printf(" %#v |", current.Data)
current = current.GetRight()
if current == d.Head { break }
}
fmt.Println()
}
func main() {
d := NewDCLL()
fmt.Println("Initial condition:")
d.PrintToRight()
fmt.Printf("\nInsert \"hello\" as head and set old head as head's left\n")
d.InsertLeft(&Node{"hello", nil, nil})
d.PrintToRight()
fmt.Printf("\nInsert \"world\" as head and set old head as head's left\n")
d.InsertLeft(&Node{"world", nil, nil})
d.PrintToRight()
fmt.Printf("\nInsert 3.14 as head and set old head as head's right\n")
d.InsertRight(&Node{3.14, nil, nil})
d.PrintToRight()
fmt.Printf("\nDelete head and set head's left as new head\n")
d.DeleteLeft()
d.PrintToRight()
fmt.Printf("\nDelete head and set head's right as new head\n")
d.DeleteRight()
d.PrintToRight()
fmt.Printf("\nDelete head and set head's right as new head\n")
d.DeleteRight()
d.PrintToRight()
for i := 1; i <= 3; i++ {
fmt.Printf("\nInsert %d as head and set old head as head's right\n",i)
d.InsertRight(&Node{i, nil, nil})
d.PrintToRight()
}
for i := 4; i <= 6; i++ {
fmt.Printf("\nInsert %d as head and set old head as head's left\n",i)
d.InsertLeft(&Node{i, nil, nil})
d.PrintToRight()
}
for i := 0; i < 2; i++ {
fmt.Printf("\nRotate left:\n")
d.RotateLeft()
d.PrintToRight()
}
fmt.Printf("\nRotate right:\n")
d.RotateRight()
d.PrintToRight()
fmt.Printf("\nPrint to right:\n")
d.PrintToRight()
fmt.Printf("\nPrint to left:\n")
d.PrintToLeft()
}