Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement stack and queue data structures in Golang Array

Shulou Source: shulou.com Published: 2022-06-01 23:52:17 10月05日 Update

In this issue, the editor will bring you about how to implement stack and queue data structures in Golang arrays. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

A stack is a collection of a series of objects with the principle of first in and then out.

Stack is the simplest and most important data structure, and its additions and deletions are operated at the top of the stack. It has the following attributes:

S.push (e): add an e element to the top of the stack

S.pop (): delete the top stack element in stack and return

S.isEmpty (): returns true if the stack is empty

S.len (): returns the length of the stack

S.top (): returns data at the top of the stack

The stack is implemented with an array:

Package main

Import (

"errors"

"fmt"

)

Var (

StackIsNil = errors.New ("empty stack")

)

Type stack [] int

/ / Push inserts data into the top of stack

Func (s * stack) Push (e int) {

* s = append (* s, e)

Return

}

/ / Pop deletes the top data of stack and returns the deleted data

Func (s * stack) Pop () (ret int, err error) {

If len (* s) = = 0 {

Return 0, stackIsNil

}

Temp: = * s

Ret = temp [len (temp)-1]

Temp = temp [: len (temp)-1]

* s = temp

Return

}

/ / IsEmpty determines whether it is empty or not

Func (s * stack) IsEmpty () bool {

Return len (* s) = = 0

}

/ / Top gets the data at the top of stack

Func (s * stack) Top () (int, error) {

If len (* s) = = 0 {

Return 0, stackIsNil

}

Temp: = * s

Return temp [len (temp)-1], nil

}

/ / Len gets the length of stack

Func (s * stack) Len () int {

Return len (* s)

}

Func main () {

S: = new (stack)

/ / insert 1

S.Push (1)

/ / insert 2

S.Push (2)

/ / insert 5

S.Push (5)

/ / get the length

Fmt.Println (s.Len ()) / / 3

/ / get the data at the top of stack

Fmt.Println (s.Top ()) / / 5

/ / Delete the top data

Fmt.Println (s.Pop ()) / / 5

/ / get the length

Fmt.Println (s.Len ()) / / 2

/ / determine whether the stack is empty

Fmt.Println (s.IsEmpty ())

}

A queue is also a collection of a series of objects with a first-in-first-out principle.

The characteristic of the queue is that access and deletion are restricted to the first element of the queue, insertion is limited to the end of the queue, and the attributes of the queue:

Q.enqueue (e): inserts an element into the end of the queue

Q.dequeue (): delete and return the first element, and report an error if the queue is empty

Q.first (): does not delete the element, but returns the first element directly. If it is empty, an error is reported.

Q.isEmpty (): return true if the queue is empty

Q.len (): returns the queue length.

Array implementation queue code:

Package main

Import (

"errors"

"fmt"

)

Var (

ErrNilQueue = errors.New ("queue is nil")

)

Type queue [] int

/ / insert data at the end of the Enqueue queue

Func (Q * queue) Enqueue (e int) {

* Q = append (* Q, e)

}

/ / Dequeue queue deletes the first element

Func (Q * queue) Dequeue () (ret int, err error) {

If len (* Q) = = 0 {

Err = ErrNilQueue

Return

}

Temp: = * Q

Ret = temp [0]

Temp = temp [1:]

* Q = temp

Return

}

/ / First returns the first data

Func (Q * queue) First () (ret int, err error) {

If len (* Q) = = 0 {

Err = ErrNilQueue

Return

}

Temp: = * Q

Ret = temp [0]

Return

}

Func (Q * queue) IsEmpty () bool {

Return len (* Q) = = 0

}

Func (Q * queue) Len () int {

Return len (* Q)

}

Func main () {

Q: = new (queue)

Q.Enqueue (1)

Q.Enqueue (10)

Q.Enqueue (20)

Fmt.Println (q.First ()) / / 1

Fmt.Println (q.Dequeue ()) / / 1

Fmt.Println (q.First ()) / / 10

Fmt.Println (q.IsEmpty ()) / / false

Fmt.Println (q.Len ()) / / 2

Fmt.Println (q.Dequeue ()) / / 10

Fmt.Println (q.Len ()) / / 1

}

This is how the Golang array shared by the editor implements the stack and queue data structures. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Data queue element length top data structure array structure tail advanced content principle object attribute analysis restriction important professional small and medium code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Linux Shulou Tech Info Xiaomi