-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSinglyCircularLinkedList.go
224 lines (195 loc) · 4.7 KB
/
SinglyCircularLinkedList.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
/*
Singly Circular Linked List Interface
*/
package main
import "fmt"
type Any interface{}
type Node struct {
Data Any
NextLink *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 next node: O(1)
func (n *Node) GetNext() *Node {
return n.NextLink
}
// Set next node: O(1)
func (n *Node) SetNext(next *Node) {
n.NextLink = next
}
// Test whether node has next node: O(1)
func (n *Node) HasNext() bool {
if n.NextLink == nil { return false }
return true
}
type SCLL struct {
Head *Node
count int
}
// Create a new Singly Circular Linked List: O(1)
func NewSCLL() *SCLL {
return &SCLL{}
}
// Return size: O(1)
func (s *SCLL) Count() int {
return s.count
}
// Return last node: O(N)
func (s *SCLL) GetTail() *Node {
if s.Head == nil { return nil }
current := s.Head
for current.GetNext() != s.Head {
current = current.GetNext()
}
return current
}
// Insert as head, set old head as head's prev: O(1)
func (s *SCLL) InsertPrev(newnode *Node) {
if s.Head == nil {
s.Head = newnode
newnode.SetNext(newnode)
s.count++
return
}
next := s.Head.GetNext()
newnode.SetNext(next)
s.Head.SetNext(newnode)
s.Head = newnode
s.count++
}
// Insert as head, set old head as head's next: O(N)
func (s *SCLL) InsertNext(newnode *Node) {
if s.Head == nil {
s.Head = newnode
newnode.SetNext(newnode)
s.count++
return
}
tail := s.GetTail()
tail.SetNext(newnode)
newnode.SetNext(s.Head)
s.Head = newnode
s.count++
}
// Delete head, set head's prev as new head: O(N)
func (s *SCLL) DeletePrev() {
if s.Head != nil {
if s.count == 1 {
s.Head = nil
s.count--
return
}
tail := s.GetTail()
tail.SetNext(s.Head.GetNext())
s.Head = tail
s.count--
}
}
// Delete head, set head's next as new head: O(N)
func (s *SCLL) DeleteNext() {
if s.Head != nil {
if s.count == 1 {
s.Head = nil
s.count--
return
}
tail := s.GetTail()
tail.SetNext(s.Head.GetNext())
s.Head = s.Head.GetNext()
s.count--
}
}
// Set head's next as new head: O(1)
func (s *SCLL) RotateLeft() {
if s.Head != nil {
s.Head = s.Head.GetNext()
}
}
// Set head's prev as new head: O(N)
func (s *SCLL) RotateRight() {
if s.Head != nil {
s.Head = s.GetTail()
}
}
// Print to left: O(N)
func (s *SCLL) PrintToLeft() {
if s.Head == nil {
fmt.Println("| Empty |")
return
}
current := s.Head
fmt.Printf("| %#v |", current.Data)
defer fmt.Println()
current = current.GetNext()
for current != s.Head {
defer fmt.Printf(" %#v |", current.Data)
current = current.GetNext()
}
}
// Print to right: O(N)
func (s *SCLL) PrintToRight() {
if s.Head == nil {
fmt.Println("| Empty |")
return
}
current := s.Head
fmt.Print("|")
for {
fmt.Printf(" %#v |", current.Data)
current = current.GetNext()
if current == s.Head { break }
}
fmt.Println()
}
func main() {
s := NewSCLL()
fmt.Println("Initial condition:")
s.PrintToRight()
fmt.Printf("\nInsert \"hello\" as head and set old head as head's prev\n")
s.InsertPrev(&Node{"hello", nil})
s.PrintToRight()
fmt.Printf("\nInsert \"world\" as head and set old head as head's prev\n")
s.InsertPrev(&Node{"world", nil})
s.PrintToRight()
fmt.Printf("\nInsert 3.14 as head and set old head as head's next\n")
s.InsertNext(&Node{3.14, nil})
s.PrintToRight()
fmt.Printf("\nDelete head and set head's prev as new head\n")
s.DeletePrev()
s.PrintToRight()
fmt.Printf("\nDelete head and set head's next as new head\n")
s.DeleteNext()
s.PrintToRight()
fmt.Printf("\nDelete head and set head's next as new head\n")
s.DeleteNext()
s.PrintToRight()
for i := 1; i <= 3; i++ {
fmt.Printf("\nInsert %d as head and set old head as head's next\n",i)
s.InsertNext(&Node{i, nil})
s.PrintToRight()
}
for i := 4; i <= 6; i++ {
fmt.Printf("\nInsert %d as head and set old head as head's prev\n",i)
s.InsertPrev(&Node{i, nil})
s.PrintToRight()
}
for i := 0; i < 2; i++ {
fmt.Printf("\nRotate left:\n")
s.RotateLeft()
s.PrintToRight()
}
fmt.Printf("\nRotate right:\n")
s.RotateRight()
s.PrintToRight()
fmt.Printf("\nPrint to right:\n")
s.PrintToRight()
fmt.Printf("\nPrint to left:\n")
s.PrintToLeft()
}