-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_linked_list.go
154 lines (122 loc) · 2.35 KB
/
simple_linked_list.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
// Package linkedlist implements a singly linked list.
package linkedlist
import "errors"
// Element holds data and a pointer to the next Element.
type Element struct {
data int
next *Element
}
// List holds the 1st element of the list and the size of the list.
type List struct {
head *Element
tail *Element
curr *Element
size int
}
// New returns a new list that is populated using the passed slice/array.
func New(slice []int) *List {
list := &List{
head: nil,
tail: nil,
curr: nil,
size: 0,
}
if len(slice) == 0 {
return list
}
for _, d := range slice {
list.Push(d)
}
return list
}
// Size returns the size of the list.
func (l *List) Size() int {
return l.size
}
// Next returns a pointer to the next item in the List.
func (l *List) Next() *Element {
if l.head == nil || l.curr == nil {
return nil
}
next := l.curr
l.curr = l.curr.next
return next
}
// Push add a new number to the end of the List.
func (l *List) Push(data int) {
element := Element{
data: data,
next: nil,
}
switch {
case l.head == nil:
l.head = &element
l.curr = l.head
l.tail = l.head
default:
l.curr = l.head
l.tail.next = &element
l.tail = &element
}
l.size++
}
// Reset resets a List to it's zero value.
func (l *List) Reset() {
l.head = nil
l.curr = nil
l.tail = nil
l.size = 0
}
// Pop returns an interger and an error code from the last element of the List while also removing it.
func (l *List) Pop() (int, error) {
if l.Size() == 0 {
return 0, errors.New("can't pop an element from an empty list")
}
if l.Size() == 1 {
data := l.head.data
l.Reset()
return data, nil
}
e := l.Next()
for e.next != nil {
if e.next.next == nil {
break
}
e = l.Next()
}
data := e.next.data
e.next = nil
l.tail = e
l.curr = l.head
l.size--
return data, nil
}
// Array returns the List as a slice.
func (l *List) Array() []int {
if l.Size() == 0 {
return []int{}
}
slice := []int{}
e := l.Next()
if e != nil {
for e.next != nil {
slice = append(slice, e.data)
e = l.Next()
}
slice = append(slice, e.data)
}
return slice
}
// Reverse returns a new List in reversed order.
func (l *List) Reverse() *List {
if l.Size() == 0 {
return &List{}
}
fSlice := l.Array()
rSlice := []int{}
for i := range fSlice {
rSlice = append(rSlice, fSlice[len(fSlice)-1-i])
}
rList := New(rSlice)
return rList
}