-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
50 lines (38 loc) · 633 Bytes
/
Copy pathstack.go
File metadata and controls
50 lines (38 loc) · 633 Bytes
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
package stack
import "fmt"
type Stack struct {
arr []int
top int
n int
}
func NewStack(n int) *Stack {
return &Stack{
arr: make([]int, n+1),
top: 0,
n: n,
}
}
func (s *Stack) Pop() (int, error) {
if s.top == 0 {
return 0, fmt.Errorf("stack underflow")
}
s.top = s.top - 1
return s.arr[s.top+1], nil
}
func (s *Stack) Push(x int) error {
if s.IsFull() {
return fmt.Errorf("stack overflow")
}
s.top = s.top + 1
s.arr[s.top] = x
return nil
}
func (s *Stack) IsEmpty() bool {
return s.top == 0
}
func (s *Stack) IsFull() bool {
return s.top == s.n
}
func (s *Stack) Size() int {
return s.top
}