-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.go
262 lines (229 loc) · 4.52 KB
/
linkedList.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"fmt"
"time"
)
type Node struct {
next *Node
key interface{}
}
type List struct {
head *Node
size int
}
type MyError struct {
When time.Time
What string
}
func (e *MyError) Error() string {
return fmt.Sprintf("at %v, %s", e.When, e.What)
}
func (list *List) empty() bool {
if list.head == nil {
return true
} else {
return false
}
}
func (list *List) Size() int {
return list.size
}
func (list *List) returnLastNode() *Node {
tempNode := list.head
for tempNode.next != nil {
tempNode = tempNode.next
}
return tempNode
}
/*
Returns the value at an index
*/
func (list *List) ValueAt(index int) (error, interface{}) {
if index > list.size-1 {
fmt.Println("Return error error")
return &MyError{
time.Now(),
"Index out of bounds",
}, nil
} else {
// fmt.Println("index and size is", index, list.size)
tempNode := list.head
for i := 0; i < index; i++ {
tempNode = tempNode.next
}
return nil, tempNode.key
}
}
/*
ValueFrom tail
*/
func (list *List) ValueFromTail(index int) interface{} {
if index > list.size {
fmt.Println("ValueFromTail: Index out of bounds")
return nil
} else {
index = list.size - index - 1
_, returnValue := list.ValueAt(index)
return returnValue
}
}
/*
To add a new head node
*/
func (list *List) PushFront(key interface{}) {
newNode := &Node{nil, key}
if list.empty() {
list.head = newNode
list.size++
} else {
newNode.next = list.head
list.head = newNode
list.size++
}
}
/*
Remove head and return the value
*/
func (list *List) PopFront() (error, interface{}) {
if list.empty() {
return &MyError{
time.Now(),
"Cannot Pop, list is empty",
}, nil
} else {
returnValue := list.head.key
list.head = list.head.next
list.size--
return nil, returnValue
}
}
/*
PopBack, to remove the last node, and return the value
*/
func (list *List) PopBack() interface{} {
if list.empty() {
fmt.Println("List is empty cannot pop back")
return nil
} else {
var previousNode *Node
tempNode := list.head
for tempNode.next != nil {
previousNode = tempNode
tempNode = tempNode.next
}
returnValue := tempNode.key
previousNode.next = nil
list.size--
return returnValue
}
}
/*
Append to add at the end of the list
*/
func (list *List) Append(key interface{}) {
newNode := &Node{nil, key}
if list.empty() {
list.head = newNode
list.size++
} else {
lastNode := list.returnLastNode()
lastNode.next = newNode
list.size++
}
}
/*
InsertAfter is to insert of a specified node
*/
func (list *List) InsertAfter(index int, key interface{}) {
newNode := &Node{nil, key}
if list.empty() {
list.head = newNode
list.size++
} else {
tempNode := list.head
for i := 0; i < index; i++ {
tempNode = tempNode.next
}
newNode.next = tempNode.next
tempNode.next = newNode
list.size++
}
}
/*
Delete the node at specified index
*/
func (list *List) DeleteIndex(index int) {
if list.empty() {
fmt.Println("List is empty cannot del")
}
}
/*
Reverses the list
*/
func (list *List) ReverseList() {
previousNode := &Node{nil, nil}
currentNode := list.head
nextNode := list.head.next
for i := 0; i < list.size-1; i++ {
fmt.Println("P:", previousNode.key, "C:", currentNode.key, "N:", nextNode.key)
fmt.Println("I is: ", i)
previousNode = currentNode
if i == 0 {
previousNode.next = nil
} else if i == list.size-2 {
list.head = nextNode
}
currentNode = nextNode
nextNode = currentNode.next
currentNode.next = previousNode
}
}
func (list *List) Display() {
tempNode := list.head
for tempNode != nil {
fmt.Printf("%+v -> ", tempNode.key)
tempNode = tempNode.next
}
fmt.Println()
fmt.Println("Linked List size is:", list.size)
}
func main() {
ll := List{}
fmt.Println("Is list empty?", ll.empty())
ll.Display()
ll.Append(5)
fmt.Println("Is list empty?", ll.empty())
ll.Display()
ll.Append(6)
ll.Display()
ll.Append(7)
ll.Append(8)
ll.Append(9)
ll.Append("Rahul")
ll.Display()
ll.InsertAfter(2, "new2")
ll.Display()
if err, value := ll.ValueAt(6); err == nil {
fmt.Println("Value at index 6 is", value)
} else {
fmt.Println(err)
}
fmt.Println("Push at front 4")
ll.PushFront(4)
ll.Display()
fmt.Println("Pushing new head")
ll.PushFront("New Head")
ll.Display()
if err, value := ll.PopFront(); err == nil {
fmt.Println("popping head", value)
} else {
fmt.Println(err)
}
ll.Display()
fmt.Println("Popping tail", ll.PopBack())
ll.Display()
fmt.Println("Value from tail ", ll.ValueFromTail(3))
ll.ReverseList()
ll.Display()
// fmt.Println("List size is ", ll.Size())
}