Data structure, Golang implementation of the stack into the stack to cover the first element

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. * * This program has a problem, the element into the stack, will overwrite the first element, the last stack left only a 2, ask the big God solution * * "" ' Gopackage mainimport ("Log" "FMT") type stack struct {size Int64// Stack capacity top Int64//stack top data []interface{}}func makestack (size Int64) Stack{s: =stack{}s.size=sizes.data =make ([]interface{} , size) return s}//into the stack, insufficient space, segment-by-section elevation of func (S *stack) Push (e interface{}) bool{if S.isfull () {log. Printf ("Stack full, unable to stack") return FALSE}S.DATA[S.TOP]=EFMT. Println (s.top) S.top++return true}//out stack, stack top lowers func (S *stack) Pop () (r interface{},err error) {if S.isempty () {err =fmt. Errorf ("Stack is empty, unable to complete the stack") log. Printf ("Stack empty, unable to complete the stack") Return}s.top--r =s.data[s.top]return}//determine if stack is full func (S *stack) isfull () Bool{return s.top==s.size} Determine if the stack is empty func (S *stack) IsEmpty () Bool{return S.top==0}func (S *stack) Traverse (FN func (R interface{}), Goorto bool) {//go True to traverse the stack false to traverse the stack if Goorto {var i int64= 0for; i<s.top;i++ {fn (s.data[i])}}else{for i:=s.top-1;i>=0;i--{fn (s.da Ta[i])}}}//stack out test//To convert decimal 1348 into octal func teststack () {var fn_c = func (n int) {s: =makestack () for {if n==0 {Break}s.push (n%8) n =n/8}s.traverse (func (R interface{}) {FMT. Print (R)},false)}fn_c (1348)}func main () {Teststack ()} "230 reads  
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.