stack_using_queues

  • 2022-12-14
  • 浏览 (584)

stack_using_queues.go 源码

package main

//用队列实现栈

type MyStack struct {
	queue []int
}

func constructor() MyStack {
	return MyStack{}
}

func (this *MyStack) Push(x int) {
	this.queue = append(this.queue, x)
	n := len(this.queue)
	if n > 1 {
		last := this.queue[n-1]
		copy(this.queue[1:], this.queue[0:n-1])
		this.queue[0] = last
	}
}

func (this *MyStack) Pop() int {
	val := this.queue[0]
	this.queue = this.queue[1:]
	return val
}

func (this *MyStack) Top() int {
	return this.queue[0]
}

func (this *MyStack) Empty() bool {
	return len(this.queue) == 0
}

你可能感兴趣的文章

design_circular_deque

design_circular_queue

sliding_window_maximum

0  赞