-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_linkedlists.go
82 lines (62 loc) · 1.64 KB
/
2_linkedlists.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
// Implementing a LinkedList data structure in go
// LinkedLists are a collection of several Nodes
// We'll implement Append, Prepend and Remove methods
// of a typical LinkedList
// LinkedLists have a Head as starting point and
// Tail as the end point which we can easily reference
package main
import (
"errors"
)
// LinkedList represents our list with it's properties
type LinkedList struct {
Head *Node
Tail *Node
Length int
}
// Append adds a node to the end of the list
func (list *LinkedList) Append(newNode *Node) {
if list.Length == 0 {
list.Head = newNode
list.Tail = newNode
} else {
lastNode := list.Tail
lastNode.Next = newNode
list.Tail = newNode
}
list.Length++
}
// Prepend adds a node to the start of the list
func (list *LinkedList) Prepend(newNode *Node) {
if list.Length == 0 {
list.Head = newNode
list.Tail = newNode
} else {
firstNode := list.Head
list.Head = newNode
newNode.Next = firstNode
}
list.Length++
}
// Remove removes a node from the list
// This has to iterate through the whole list
// to try find the node to remove
// Can be performance costly with large lists
// You might choose to use Arrays in this case
// where we can identify an element by Index
func (list *LinkedList) Remove(node *Node) {
if list.Length == 0 {
panic(errors.New("cannot remove element on an empty list"))
}
var previousPost *Node
currentPost := list.Head
for currentPost.Value != node.Value {
if currentPost.Next == nil {
panic(errors.New("no such element found with value"))
}
previousPost = currentPost
currentPost = currentPost.Next
}
previousPost.Next = currentPost.Next
list.Length--
}