-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.go
51 lines (37 loc) · 939 Bytes
/
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
package main
import (
"fmt"
"strconv"
)
func get(top int, data [3]string) {
fmt.Println("\n", top, data[0:top])
}
func push(top int, data [3]string, maximumSize int, newItem string) (int, [3]string) {
if top < maximumSize {
data[top] = newItem
top++
get(top, data)
} else {
fmt.Println("\n Stack ("+strconv.Itoa(top)+") is full, can't add", newItem)
}
return top, data
}
func pop(top int, data [3]string) int {
if top != 0 {
top--
get(top, data)
}
return top
}
func main() {
var top int = 0
var data = [3]string{}
var maximumSize int = 3
top, data = push(top, data, maximumSize, "Kevin")
top, data = push(top, data, maximumSize, "Sally")
top, data = push(top, data, maximumSize, "David")
top, data = push(top, data, maximumSize, "Sonya")
top = pop(top, data)
top, data = push(top, data, maximumSize, "Jessy")
top, data = push(top, data, maximumSize, "Harry")
}