blob: 718e526db94f6b63ce6fd184e708993d66036ba8 (
plain) (
blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package main
import (
"fmt"
"github.com/emirpasic/gods/stacks/arraystack"
)
type MyQueue struct {
s1 *arraystack.Stack
s2 *arraystack.Stack
}
func Constructor() MyQueue {
s1 := arraystack.New()
s2 := arraystack.New()
q := MyQueue{s1: s1, s2: s2}
return q
}
func (this *MyQueue) Push(x int) {
this.s1.Push(x)
}
func (this *MyQueue) Pop() int {
for {
if val, ok := this.s1.Pop(); ok {
this.s2.Push(val)
} else {
break
}
}
val, _ := this.s2.Pop()
for {
if val, ok := this.s2.Pop(); ok {
this.s1.Push(val)
} else {
break
}
}
return val.(int)
}
func (this *MyQueue) Peek() int {
for {
if val, ok := this.s1.Pop(); ok {
this.s2.Push(val)
} else {
break
}
}
val, _ := this.s2.Pop()
this.s2.Push(val)
for {
if val, ok := this.s2.Pop(); ok {
this.s1.Push(val)
} else {
break
}
}
return val.(int)
}
func (this *MyQueue) Empty() bool {
return this.s1.Empty()
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
func main() {
obj := Constructor()
obj.Push(1)
obj.Push(2)
fmt.Println(obj.Peek())
fmt.Println(obj.Pop())
fmt.Println(obj.Empty())
}
|