-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathqueue_using_stacks.go
43 lines (36 loc) · 969 Bytes
/
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
package queue
import "errors"
// ErrStackEmpty occurs when popping an empty stack.
var ErrStackEmpty = errors.New("empty stack")
type (
// UsingStacks is a queue that is made using two stacks.
UsingStacks struct {
stack1 Stack
stack2 Stack
}
// Stack is a stack of integers.
Stack struct {
stack []int
}
)
// enqueue solves the problem in O(1) time and O(n) space.
func (usingStacks *UsingStacks) enqueue(n int) {
usingStacks.stack1.push(n)
}
// dequeue solves the problem in O(1) time and O(n) space.
func (usingStacks *UsingStacks) dequeue() int {
if len(usingStacks.stack2.stack) == 0 {
for len(usingStacks.stack1.stack) != 0 {
usingStacks.stack2.push(usingStacks.stack1.pop())
}
}
return usingStacks.stack2.pop()
}
func (stack *Stack) push(element int) {
stack.stack = append(stack.stack, element)
}
func (stack *Stack) pop() int {
tmp := stack.stack[len(stack.stack)-1]
stack.stack = stack.stack[:len(stack.stack)-1]
return tmp
}