queue_using_stack
queue_using_stack.go 源码
package main
// 用栈实现队列
type MyQueue struct {
input []int
output []int
}
func constructor() MyQueue {
return MyQueue{}
}
func (this *MyQueue) Push(x int) {
this.input = append(this.input, x)
}
func (this *MyQueue) Pop() int {
this.Peek()
res := this.output[len(this.output)-1]
this.output = this.output[:len(this.output)-1]
return res
}
func (this *MyQueue) Peek() int {
if len(this.output) == 0 {
for _, v := range this.input {
this.output = append([]int{v}, this.output...)
}
this.input = []int{}
}
return this.output[len(this.output)-1]
}
func (this *MyQueue) Empty() bool {
return len(this.input) == 0 && len(this.output) == 0
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦