-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathstack.go
More file actions
54 lines (47 loc) · 1.19 KB
/
stack.go
File metadata and controls
54 lines (47 loc) · 1.19 KB
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
// Package stack provides an implementation of a LIFO stack built using a
// resizing array.
package stack
// Stack implements a LIFO stack with peeking.
type Stack[T any] struct {
entries []T
}
// New returns an empty stack.
func New[T any]() *Stack[T] {
return &Stack[T]{
entries: nil,
}
}
// Push places 'value' at the top of the stack.
func (s *Stack[T]) Push(value T) {
s.entries = append(s.entries, value)
}
// Pop removes the stack's top element and returns it. If the stack is empty it
// returns the zero value.
func (s *Stack[T]) Pop() (t T) {
if len(s.entries) == 0 {
return t
}
v := s.entries[len(s.entries)-1]
s.entries = s.entries[:len(s.entries)-1]
return v
}
// Peek returns the stack's top element but does not remove it. If the stack is
// empty the zero value is returned.
func (s *Stack[T]) Peek() (t T) {
if len(s.entries) == 0 {
return t
}
return s.entries[len(s.entries)-1]
}
// Size returns the number of elements in the stack.
func (s *Stack[T]) Size() int {
return len(s.entries)
}
// Copy returns a copy of this stack.
func (s *Stack[T]) Copy() *Stack[T] {
entries := make([]T, len(s.entries))
copy(entries, s.entries)
return &Stack[T]{
entries: entries,
}
}