1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| type MyStack struct { queue []int }
func Constructor() MyStack { return MyStack{ queue: make([]int, 0), } }
func (this *MyStack) Push(x int) { this.queue = append(this.queue, x) }
func (this *MyStack) Pop() int { n := len(this.queue) - 1 for n != 0 { val := this.queue[0] this.queue = this.queue[1:] this.queue = append(this.queue, val) n-- } val := this.queue[0] this.queue = this.queue[1:] return val }
func (this *MyStack) Top() int { val := this.Pop() this.queue = append(this.queue, val) return val }
func (this *MyStack) Empty() bool { return len(this.queue) == 0 }
|