-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
59 lines (44 loc) · 1.07 KB
/
stack.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
package stack
import "errors"
// ErrEmptyStack is returned when the stack is empty.
var ErrEmptyStack = errors.New("stack is empty")
// Stack represents a stack of items of type T.
type Stack[T any] struct {
data []T
}
// New creates a new, empty stack of items of type T.
func New[T any]() *Stack[T] {
return &Stack[T]{
data: make([]T, 0),
}
}
// Push adds an item to the stack.
func (s *Stack[T]) Push(item T) {
s.data = append(s.data, item)
}
// Pop removes an item from the stack.
func (s *Stack[T]) Pop() (T, error) {
var item T
if s.IsEmpty() {
return item, ErrEmptyStack
}
item = s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
return item, nil
}
// Peek returns the item at the top of the stack.
func (s *Stack[T]) Peek() (T, error) {
var item T
if s.IsEmpty() {
return item, ErrEmptyStack
}
return s.data[len(s.data)-1], nil
}
// Size returns the size of the stack.
func (s *Stack[T]) Size() int {
return len(s.data)
}
// IsEmpty returns true if the stack is empty, false otherwise.
func (s *Stack[T]) IsEmpty() bool {
return len(s.data) == 0
}