File tree 1 file changed +37
-0
lines changed
src/0201-0300/232 - Implement Queue using Stacks
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ type MyQueue struct {
4
+ stack []int
5
+ }
6
+
7
+ func Constructor () MyQueue {
8
+ return MyQueue {}
9
+ }
10
+
11
+ // Push element x to the back of queue.
12
+ func (this * MyQueue ) Push (x int ) {
13
+ this .stack = append (this .stack , x )
14
+ }
15
+
16
+ // Removes and returns the element at the front of queue.
17
+ func (this * MyQueue ) Pop () int {
18
+ if len (this .stack ) == 0 {
19
+ return 0
20
+ }
21
+ pop := this .stack [0 ]
22
+ this .stack = this .stack [1 :]
23
+ return pop
24
+ }
25
+
26
+ // Get the front element.
27
+ func (this * MyQueue ) Peek () int {
28
+ if len (this .stack ) == 0 {
29
+ return 0
30
+ }
31
+ return this .stack [0 ]
32
+ }
33
+
34
+ // Return whether the queue is empty.
35
+ func (this * MyQueue ) Empty () bool {
36
+ return len (this .stack ) == 0
37
+ }
You can’t perform that action at this time.
0 commit comments