-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (58 loc) · 1.27 KB
/
main.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import "fmt"
type MyStack struct {
queue []int // 用数组实现一个队列的功能
}
func Constructor() MyStack {
return MyStack{
queue: make([]int, 0),
}
}
// 将元素 `x` 压入栈顶。
func (this *MyStack) Push(x int) {
// 直接把元素加入队列即可
this.queue = append(this.queue, x)
}
// 移除并返回栈顶元素
func (this *MyStack) Pop() int {
if len(this.queue) == 0 {
return 0
}
// 弹出除队尾元素的所有元素,并重新追加到队列中
n := len(this.queue) - 1
for n != 0 {
// 模拟队列头弹出元素,并且把队列头元素重新加入队列尾部
x := this.queue[0]
this.queue = this.queue[1:]
this.queue = append(this.queue, x)
n--
}
x := this.queue[0]
this.queue = this.queue[1:]
return x
}
// 返回栈顶元素
func (this *MyStack) Top() int {
x := this.Pop()
if x == 0 {
return x
}
this.Push(x)
return x
}
// 如果栈是空的,返回 `true` ;否则,返回 `false`
func (this *MyStack) Empty() bool {
if len(this.queue) != 0 {
return false
}
return true
}
func main() {
println("UseCase 1......")
stack := Constructor()
stack.Push(1)
stack.Push(2)
fmt.Printf("Peek: %v \n", stack.Top())
fmt.Printf("Pop: %v \n", stack.Pop())
fmt.Printf("Empty: %v \n", stack.Empty())
}