forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
69 lines (58 loc) · 1.67 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
60
61
62
63
64
65
66
67
68
69
package stl4go
// Stack s is a container adaptor that provides the functionality of a stack,
// a LIFO (last-in, first-out) data structure.
type Stack[T any] struct {
elements []T
}
// NewStack creates a new Stack object.
func NewStack[T any]() *Stack[T] {
return &Stack[T]{nil}
}
// NewStackCap creates a new Stack object with the specified capicity.
func NewStackCap[T any](capicity int) *Stack[T] {
return &Stack[T]{make([]T, 0, capicity)}
}
// IsEmpty implements the Container interface.
func (s Stack[T]) IsEmpty() bool {
return len(s.elements) == 0
}
// Len implements the Container interface.
func (s Stack[T]) Len() int {
return len(s.elements)
}
// Cap returns the capacity of the stack.
func (s Stack[T]) Cap() int {
return cap(s.elements)
}
// Clear implements the Container interface.
func (s *Stack[T]) Clear() {
s.elements = s.elements[0:0]
}
// Push pushes the element to the top of the stack.
func (s *Stack[T]) Push(t T) {
s.elements = append(s.elements, t)
}
// TryPop tries to popup an element from the top of the stack.
func (s *Stack[T]) TryPop() (val T, ok bool) {
var t T
if len(s.elements) == 0 {
return t, false
}
t = s.elements[len(s.elements)-1]
s.elements = s.elements[:len(s.elements)-1]
return t, true
}
// Pop popups an element from the top of the stack.
// It must be called when IsEmpty() returned false,
// otherwise it will panic.
func (s *Stack[T]) Pop() T {
t := s.elements[len(s.elements)-1]
s.elements = s.elements[:len(s.elements)-1]
return t
}
// Top returns the top element in the stack.
// It must be called when s.IsEmpty() returned false,
// otherwise it will panic.
func (s Stack[T]) Top() T {
return s.elements[len(s.elements)-1]
}