The stack _golang of the Go language implementation sequential storage

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.