The stack of GO data structures

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, what is the stack

This is the road map of Hangzhou metro line Line 1. Brother David, how did the subway train turn around?

I am not suspense, the train usually uses "Herringbone track" to change lanes.

The train first entered the "Herringbone track" from track A, and then the B-track was opened. From a track to enter the time of the car 1th is the front, from the B out of the time of the car 4th will become the front. So we see a front and rear of the subway. "Herringbone track" has a feature: Advanced, the English shorthand is filo, meaning their own experience. The computer experts abstracted the "herringbone track" and put forward a concept called "stack".

Stacks and "herringbone tracks" are characterised by the same FIFO, except that the compartments are replaced with data. Next, see David brother to the stack of the pants left.

Second, the structure of the stack

Brother David split the stack into containers and lists of two pieces, the container with the structure of the implementation, linked list with a single linked list, of course, you can also use other linked list structure, even arrays to achieve.

Third, the interface description and implementation

1. Init

Initializing the stack is actually initializing the list inside.

func (stack *Stack) Init() {    lst := new(List)    (*stack).list = lst    lst.Init()}

2. Push

Data into the stack, also known as the pressure stack, is to drive in the car. Brother David put the new car in the chain, you can also put the tail, as long as you happy.

func (stack *Stack) Push(data Object) bool {    lst := (*stack).list    return lst.InsertAtHead(data) // 车子开进去}

3. Pop

Data out of the stack, is to drive out the car, of course, from the list of the head open.

func (stack *Stack) Pop() Object {    lst := (*stack).list    return lst.RemoveAt(0) // 从链表头把车子开出来}

4. Peek

From time to time to peek under the current stack of the nearest compartment, I do not have a voyeur, only to see hands.

func (stack *Stack) Peek() Object {    lst := (*stack).list    return lst.First()}

5, GetSize

Alas, after all, the land is the most expensive, so can not indefinitely put in the car, to grasp the number of the stack in real-time, once too much, it is necessary to control.

func (stack *Stack) GetSize() uint64 {    lst := (*stack).list    return lst.GetSize()}

Iv. Summary
The careful observation of the classmate can be found that David's stack and out of the stack of the order is from the single-linked table header start.

Everyone can try to squeeze from the tail and out of the stack.

You can also try to use a doubly linked list, a circular list or even an array, in short, as long as you are happy. Too much, the next, brother David wants to talk about the queue, this structure has been very hot, in the event processing, high-throughput systems have excellent performance.

Code download

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.