The examples in this article describe the stacks of go implementation sequential storage. Share to everyone for your reference. Specifically as follows:
1. The Sequence.go code is as follows:
Copy Code code as follows:
////////
Sequentially stored stacks
////////
Package sequence
Const MAXSIZE = 20
Type Stack struct {
Data [maxsize]int//Storage stack Element
Top int//point to top of stack, always pointing to upper element, empty time-1
}
Pressure stack
D: Stack elements
Func (S *stack) Push (d int) bool {
If s.top+1 > MAXSIZE {
return False
}
S.DATA[S.TOP+1] = d
s.top++
return True
}
Bomb stack
Func (S *stack) Pop () int {
if s.top = =-1 {
return 0
}
S.data[s.top] = 0
D: = S.data[s.top]
s.top--
Return D
}
The capacity to take the stack
Func (S *stack) getvol () int {
Return Len (s.data)
}
Take the length of the stack
Func (S *stack) getlength () int {
c: = s.top + 1
Return C
}
2. The Main.go code is as follows:
Copy Code code as follows:
Package Main
Import (
"FMT"
"Stack/sequence"
)
Func Main () {
Initialize a stack
var s sequence. Stack
S.top =-1
Press into 10 elements
For i: = 1; I <= 10; i++ {
S.push (i)
}
Fmt. PRINTLN (s)
Fmt. Println (S.getvol ())//capacity
Fmt. Println (S.getlength ())//length
Pop Up an element
S.pop ()
S.pop ()
Fmt. PRINTLN (s)
Fmt. Println (S.getvol ())//capacity
Fmt. Println (S.getlength ())//length
}
Hopefully this article will help you with your go language program.