go Stack 简单实现

来源:互联网 发布:乐乎城市青年社区 亦庄 编辑:程序博客网 时间:2024/05/21 07:00
package mainimport ("fmt")type Stack struct {top  intdata [10]int}func initStack(s *Stack) {s.top = 0}func push(s *Stack, element int) {s.data[s.top] = elements.top++}func pop(s *Stack) int {s.top--return s.data[s.top]}func showStack(s *Stack) {for i := s.top; i >= 0; i-- {fmt.Printf("element %d is %d \t", i, s.data[i])}}func showDiscribe() {fmt.Println("***********operations description****************")fmt.Println("")fmt.Println("press 1 push element to Stack")fmt.Println("press 2 pop element to Stack")fmt.Println("press 3 show elements to Stack")fmt.Println("press 4 show operations")fmt.Println("press 5 exit")fmt.Println("")fmt.Println("*************************************************")}func main() {showDiscribe()var op intvar value ints := new(Stack)initStack(s)for {fmt.Scanf("%d", &op)switch op {case 1:fmt.Println("please input the vaule")fmt.Scanf("%d", &value)push(s, value)case 2:fmt.Println("the value is :", pop(s))case 3:fmt.Println("all elements :")showStack(s)case 4:showDiscribe()case 5:goto Exit}}Exit:}

0 0