-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_stack.go
57 lines (47 loc) · 992 Bytes
/
string_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
package parser
import (
"errors"
)
var (
ErrorStackEmpty = errors.New("stack empty")
)
type StringStack struct {
items []string
}
func (receiver *StringStack) Push(value string) {
receiver.items = append(receiver.items, value)
}
func (receiver *StringStack) Pop() (string, error) {
if receiver.IsEmpty() {
return "", ErrorStackEmpty
}
value := receiver.items[0]
receiver.items = receiver.items[1:]
return value, nil
}
func (receiver *StringStack) PopOrEmpty() string {
value, err := receiver.Pop()
if err != nil {
return ""
}
return value
}
func (receiver *StringStack) Peek() (string, error) {
if receiver.IsEmpty() {
return "", ErrorStackEmpty
}
return receiver.items[0], nil
}
func (receiver *StringStack) PeekOrEmpty() string {
value, err := receiver.Peek()
if err != nil {
return ""
}
return value
}
func (receiver *StringStack) Size() int {
return len(receiver.items)
}
func (receiver *StringStack) IsEmpty() bool {
return receiver.Size() == 0
}