Skip to content

Commit 128b72e

Browse files
committed
solved: 225. Implement Stack using Queues
Signed-off-by: rajput-hemant <[email protected]>
1 parent af81b5b commit 128b72e

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 MyStack struct {
4+
queue []int
5+
}
6+
7+
func Constructor() MyStack {
8+
return MyStack{}
9+
}
10+
11+
// Push x onto stack.
12+
func (this *MyStack) Push(x int) {
13+
this.queue = append(this.queue, x)
14+
}
15+
16+
// Removes and returns the element on top of the stack
17+
func (this *MyStack) Pop() int {
18+
if len(this.queue) == 0 {
19+
return 0
20+
}
21+
pop := this.queue[len(this.queue)-1]
22+
this.queue = this.queue[:len(this.queue)-1]
23+
return pop
24+
}
25+
26+
// Get the top element.
27+
func (this *MyStack) Top() int {
28+
if len(this.queue) == 0 {
29+
return 0
30+
}
31+
return this.queue[len(this.queue)-1]
32+
}
33+
34+
// Return whether the stack is empty.
35+
func (this *MyStack) Empty() bool {
36+
return len(this.queue) == 0
37+
}

0 commit comments

Comments
 (0)