-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimplement_stack_using_queues.go
66 lines (54 loc) · 1.37 KB
/
implement_stack_using_queues.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
package main
// // Runtime: 0 ms, faster than 100.00% of Go online submissions for Implement Stack using Queues.
// // Memory Usage: 1.9 MB, less than 82.31% of Go online submissions for Implement Stack using Queues.
// /*
// type MyStack struct {
// queue []int
// }
// /** Initialize your data structure here. */
// func Constructor() MyStack {
// return MyStack{[]int{}}
// }
// /** Push element x onto stack. */
// func (this *MyStack) Push(x int) {
// this.queue = append(this.queue[:0], append([]int{x}, this.queue[0:]...)...) // prepend
// }
// /** Removes the element on top of the stack and returns that element. */
// func (this *MyStack) Pop() int {
// temp := this.queue[0]
// this.queue = this.queue[1:]
// return temp
// }
// /** Get the top element. */
// func (this *MyStack) Top() int {
// return this.queue[0]
// }
// /** Returns whether the stack is empty. */
// func (this *MyStack) Empty() bool {
// return len(this.queue) == 0
// }
type MyStack struct {
top int
stack [100]int
}
func Constructor() MyStack {
return MyStack{
top: -1,
stack: [100]int{},
}
}
func (this *MyStack) Push(x int) {
this.top++
this.stack[this.top] = x
}
func (this *MyStack) Pop() int {
var temp int = this.stack[this.top]
this.top--
return temp
}
func (this *MyStack) Top() int {
return this.stack[this.top]
}
func (this *MyStack) Empty() bool {
return this.top == -1
}