Skip to content

Commit 3cb7aaf

Browse files
committed
solved: 232. Implement Queue using Stacks
Signed-off-by: rajput-hemant <[email protected]>
1 parent 128b72e commit 3cb7aaf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)