In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.