blob: 4dbb72c0fb357f7fe2a5968c1f07dbe5ba547ecf (
plain)
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 stack
type Stack struct {
size int
height int
data []int
next *Stack // Pointer to the next element in the stack stack
}
func NewStack() *Stack {
return &Stack{
size: 32,
height: 0,
data: make([]int, 32),
next: nil,
}
}
func (s *Stack) Clear() {
s.height = 0
}
func (s *Stack) Duplicate() {
if s.height > 0 {
s.Push(s.data[s.height-1])
}
}
func (s *Stack) Pop() int {
if s.height > 0 {
s.height--
return s.data[s.height]
}
return 0
}
func (s *Stack) Push(value int) {
if s.height >= s.size {
s.size += 32
s.data = append(s.data, make([]int, 32)...)
}
s.data[s.height] = value
s.height++
}
func (s *Stack) Swap() {
a := s.Pop()
b := s.Pop()
s.Push(a)
s.Push(b)
}
|