-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.go
More file actions
47 lines (36 loc) · 954 Bytes
/
stack.go
File metadata and controls
47 lines (36 loc) · 954 Bytes
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
package uni_filter
type Stack[T any] struct {
entries []T
}
// NewStack creates a new stack object.
func NewStack[T any]() *Stack[T] {
return &Stack[T]{}
}
// Clear removes all data from the stack
func (s *Stack[T]) Clear() {
s.entries = []T{}
}
// IsEmpty returns true if the stack is empty.
func (s *Stack[T]) IsEmpty() bool {
return len(s.entries) == 0
}
// Size retrieves the number of entries stored upon the stack.
func (s *Stack[T]) Size() int {
return len(s.entries)
}
// Push appends the specified value to the stack.
func (s *Stack[T]) Push(v T) {
s.entries = append(s.entries, v)
}
// Pop removes a value from the stack.
func (s *Stack[T]) Pop() (v T) {
// get the last entry.
result := s.entries[len(s.entries)-1]
// remove it
s.entries = s.entries[:len(s.entries)-1]
return result
}
// Top return the latest value from the stack, but not remove it
func (s *Stack[T]) Top() (v T) {
return s.entries[len(s.entries)-1]
}