Go Transformation--Elementary (quad) stacks and queues for data structures

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. **1**. Stacks and queue stacks and queues are two commonly used linear structures, from a data structure perspective, stacks and queues are also linear tables, which are special in that the basic operations of stacks and queues implement a subset of gender operations, which are linear tables with limited operations and are therefore also known as qualified data structures. But from the data type point of view, they are very different from the linear table. * * Stack *: is a linear table that restricts inserts or deletions only at the end of the footer. For the stack, the end of the table has a special meaning, called the top of the stack, the corresponding header segment is called the bottom of the stack. Stacks that do not contain any elements are called empty stacks. Suppose S= (A (1), A (2), a (3),....... A (n)), we call a (1) a stack-bottom element, and a (n) is the top element of the stack. The stacking order should be a (1), A (2), a (3),....... A (n), the first element of the stack should be the top element of the stack. In other words, the stack is modified in accordance with the principle of last-first-out, so the stack is called the LIFO table (LIFO structure). [Image.png] The abstract data type of the (Https://static.studygolang.com/180123/3b2d5183466e49afac6e79e473dfcd7d.png) station is defined as follows:! [Image.png] (https://static.studygolang.com/180123/168212541682d9311da3eca703320576.png) **2**. Stack representations and implementations are similar to linear tables, and there are two ways to store representations of the industry. * * Sequential Stack * *: The sequential storage structure of a stack is to store data elements from the bottom of the stack to the top of the stack using a contiguous set of storage units, and also sets the pointer top to indicate the position of the top element of the stack in the sequence stack, and top=0 to represent the empty stack. Also, because the stack in the actual use of the size is not easy to estimate, so we initialize the empty stack should not limit the maximum capacity of the stack, the stack should be allocated a basic capacity, and then in the application process, when the stack of space, and then expand by paragraph. [Image.png] (https://static.studygolang.com/180123/ba1a6cec2746f73d06c662678107fd36.png) * * Link Stack * *: operates like a linear list, without moving elements changes only the contents of the Pointer field. **3**. Stack's actual application ' Gopackage mainimport ("Log" "FMT") type stack struct {size int64//stack capacity top Int64//stack top data []interface{}} FuncMakestack (size Int64) Stack{s: =stack{}s.size=sizes.data =make ([]interface{},size) return s}//into the stack, insufficient space, segment-wise 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 ()} "This program still has a problem, when the element is in the stack, it will overwrite the first element, the last stack only left a 2, ask the big God answer 354 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.