-
Notifications
You must be signed in to change notification settings - Fork 1
/
arraystack.go
90 lines (76 loc) · 1.87 KB
/
arraystack.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package container
import "fmt"
// ArrayStack is an implementation of Stack using slice
type ArrayStack[T any] struct {
arr []T
}
// NewArrayStack returns a new ArrayStack object
func NewArrayStack[T any]() *ArrayStack[T] {
return &ArrayStack[T]{arr: make([]T, 0)}
}
// Push a new element to the ArrayStack
func (s *ArrayStack[T]) Push(x T) {
s.arr = append(s.arr, x)
}
// Pop removes the element at the top of the ArrayStack and return the element.
// the ArrayStack must not be empty.
func (s *ArrayStack[T]) Pop() T {
if s.Len() == 0 {
panic("stack is empty")
}
n := len(s.arr)
x := s.arr[n-1]
s.arr = s.arr[:n-1]
return x
}
// Top returns the element at the top of the ArrayStack.
// the ArrayStack must not be empty.
func (s *ArrayStack[T]) Top() T {
if s.Len() == 0 {
panic("stack is empty")
}
return s.arr[s.Len()-1]
}
// Clear all elements from the ArrayStack.
func (s *ArrayStack[T]) Clear() {
s.arr = s.arr[:0]
}
// Len returns the size of the ArrayStack.
func (s *ArrayStack[T]) Len() int {
return len(s.arr)
}
/////////////////////////////
///////// Testing ///////////
/////////////////////////////
func checkArrayStackSize(s *ArrayStack[int], expect int) {
if s.Len() != expect {
panic(fmt.Sprintf("size check failed, got %d, expect %d", s.Len(), expect))
}
}
func checkArrayStackNum(got, expect int) {
if got != expect {
panic(fmt.Sprintf("answer does not match, got %d, expect %d", got, expect))
}
}
func testArrayStack() {
s := NewArrayStack[int]()
checkArrayStackSize(s, 0)
s.Push(1)
checkArrayStackSize(s, 1)
checkArrayStackNum(s.Top(), 1)
s.Pop()
checkArrayStackSize(s, 0)
s.Push(1)
s.Push(2)
s.Push(3)
checkArrayStackSize(s, 3)
checkArrayStackNum(s.Top(), 3)
s.Pop()
checkArrayStackSize(s, 2)
checkArrayStackNum(s.Top(), 2)
s.Pop()
checkArrayStackSize(s, 1)
checkArrayStackNum(s.Top(), 1)
s.Clear()
checkArrayStackSize(s, 0)
}