- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimplement_queue_using_stacks.go
71 lines (59 loc) · 1.06 KB
/
implement_queue_using_stacks.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
67
68
69
70
71
package main
typeMyQueuestruct {
stack1Stack
stack2Stack
}
funcConstructor() MyQueue {
return*new(MyQueue)
}
func (this*MyQueue) Push(xint) {
for!this.stack1.IsEmpty() {
pop, _:=this.stack1.Pop()
this.stack2.Push(pop)
}
this.stack1.Push(x)
for!this.stack2.IsEmpty() {
pop, _:=this.stack2.Pop()
this.stack1.Push(pop)
}
}
func (this*MyQueue) Pop() int {
pop, _:=this.stack1.Pop()
returnpop
}
func (this*MyQueue) Peek() int {
peek, _:=this.stack1.Peek()
returnpeek
}
func (this*MyQueue) Empty() bool {
returnthis.stack1.IsEmpty()
}
typeStack []int
func (s*Stack) IsEmpty() bool {
returnlen(*s) ==0
}
func (s*Stack) Push(numint) {
*s=append(*s, num)
}
func (s*Stack) Len() int {
returnlen(*s)
}
func (s*Stack) Pop() (int, bool) {
ifs.IsEmpty() {
return0, false
} else {
index:=len(*s) -1
elem:= (*s)[index]
*s= (*s)[:index]
returnelem, true
}
}
func (s*Stack) Peek() (int, bool) {
ifs.IsEmpty() {
return0, false
} else {
index:=len(*s) -1
elem:= (*s)[index]
returnelem, true
}
}