package main import ( "fmt" ) type Stack struct { top int data [10]int } func initStack(s *Stack) { s.top = 0 } func push(s *Stack, element int) { s.data[s.top] = element s.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 int var value int s := 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: }
有疑问加站长微信联系(非本文作者)