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
| package main
import ( "bufio" "fmt" "os" "strconv" "strings" )
var s []int
func s_push(x int) { s = append(s, x) }
func s_pop() {
if s == nil { fmt.Printf("%d\n", -1) } else if len(s) == 1 { top := s[0] fmt.Printf("%d\n", top) s = nil } else { l := len(s) top := s[l-1] fmt.Printf("%d\n", top) s = s[:l-1] } }
func s_size() { l := len(s) fmt.Printf("%d\n", l) }
func s_empty() { if s == nil { fmt.Printf("%d\n", 1) } else { fmt.Printf("%d\n", 0) } }
func s_top() { if s == nil { fmt.Printf("%d\n", -1) } else { l := len(s) top := s[l-1] fmt.Printf("%d\n", top) } }
func main() { var n int inputReader := bufio.NewReader(os.Stdin) fmt.Scanf("%d", &n)
for i := 0; i < n+1; i++ { input, _ := inputReader.ReadString('\n')
order := strings.Split(input, " ")
action := strings.TrimSpace(order[0])
if len(order) == 2 { n := strings.TrimSpace(order[1]) num, _ := strconv.Atoi(n) s_push(num) } else { switch action { case "pop": s_pop() case "size": s_size() case "empty": s_empty() case "top": s_top() } } } }
|