generated from denpeshkov/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
51 lines (39 loc) · 960 Bytes
/
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
// Package slice contains an implementation of a stack backed by a slice.
package slice
// Stack represents a stack.
type Stack[T any] struct {
e []T
}
// Len returns the number of elements in the stack.
func (s *Stack[T]) Len() int {
return len(s.e)
}
// Empty returns whether the stack is empty.
func (s *Stack[T]) Empty() bool {
return len(s.e) == 0
}
// Push adds an element onto the top of the stack.
func (s *Stack[T]) Push(x T) {
s.e = append(s.e, x)
}
// Pop removes and returns the element at the top of the stack.
// If the stack is empty - default value for element's type is returned.
func (s *Stack[T]) Pop() T {
var x T
if s.Empty() {
return x
}
n := len(s.e)
x = s.e[n-1]
s.e = s.e[:n-1]
return x
}
// Peek returns the element at the top of the stack.
// If the stack is empty - default value for element's type is returned.
func (s *Stack[T]) Peek() T {
var x T
if s.Empty() {
return x
}
return s.e[len(s.e)-1]
}