-
Notifications
You must be signed in to change notification settings - Fork 30
/
datastructs.go
156 lines (126 loc) · 2.73 KB
/
datastructs.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
package alg
import "reflect"
type Index interface {
Index(int) (int, bool)
}
type Stack interface {
Index
Clear()
Push(int)
Pop() (int, bool)
Peek() (int, bool)
Size() int
Copy() Stack
Equal(Stack) bool
}
type Queue interface {
Index
Clear()
Push(int)
Enqueue(int)
Dequeue() (int, bool)
Pop() (int, bool)
Peek() (int, bool)
Size() int
Copy() Queue
Equal(Queue) bool
}
type StackArray struct {
Array []int
}
var _ Stack = &StackArray{}
func (s *StackArray) Equal(other Stack) bool {
return reflect.DeepEqual(s, other)
}
func (s *StackArray) Clear() {
s.Array = s.Array[0:0]
}
func (s *StackArray) Push(val int) {
s.Array = append(s.Array, val)
}
func (s *StackArray) Pop() (int, bool) {
if s.Size() == 0 {
return 0, false
}
retval := s.Array[len(s.Array)-1]
s.Array = s.Array[:len(s.Array)-1]
return retval, true
}
func (s *StackArray) Index(index int) (int, bool) {
if index >= s.Size() {
return 0, false
}
return s.Array[len(s.Array)-1-index], true
}
func (s *StackArray) Peek() (int, bool) {
result, exists := s.Index(0)
return result, exists
}
func (s *StackArray) Size() int {
return len(s.Array)
}
func (s *StackArray) Copy() Stack {
newArray := make([]int, len(s.Array), cap(s.Array))
copy(newArray, s.Array)
newStack := Stack(&StackArray{newArray})
return newStack
}
func NewStackArray(size int) *StackArray {
return &StackArray{make([]int, 0, size)}
}
type QueueSlice struct {
slice []int
hasDequeued bool
}
var _ Queue = &QueueSlice{}
func (q *QueueSlice) Clear() {
q.slice = q.slice[0:0]
}
func (q *QueueSlice) Equal(other Queue) bool {
return reflect.DeepEqual(q, other)
}
func (q *QueueSlice) Enqueue(val int) {
// if q.hasDequeued {
// panic("Can't Enqueue after Dequeue")
// }
q.slice = append(q.slice, val)
}
func (q *QueueSlice) Dequeue() (int, bool) {
if q.Size() == 0 {
return 0, false
}
retval := q.slice[0]
q.slice = q.slice[1:]
return retval, true
}
func (q *QueueSlice) Index(index int) (int, bool) {
if index >= q.Size() {
return 0, false
}
return q.slice[index], true
}
func (q *QueueSlice) Peek() (int, bool) {
result, exists := q.Index(0)
return result, exists
}
// Pop is mapped to the top of the queue, so it acts like a dequeue
func (q *QueueSlice) Pop() (int, bool) {
return q.Dequeue()
}
func (q *QueueSlice) Push(val int) {
oldSlice := q.slice
q.slice = make([]int, 1+len(oldSlice), 1+cap(oldSlice))
q.slice[0] = val
copy(q.slice[1:], oldSlice)
}
func (q *QueueSlice) Size() int {
return len(q.slice)
}
func (q *QueueSlice) Copy() Queue {
newSlice := make([]int, len(q.slice), cap(q.slice))
copy(newSlice, q.slice)
return &QueueSlice{newSlice, q.hasDequeued}
}
func NewQueueSlice(size int) *QueueSlice {
return &QueueSlice{make([]int, 0, size), false}
}