-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0232-implement-queue-using-stacks.go
More file actions
78 lines (62 loc) · 1.44 KB
/
0232-implement-queue-using-stacks.go
File metadata and controls
78 lines (62 loc) · 1.44 KB
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
72
73
74
75
76
77
78
package design
/*
Problem:
Implement the class Queue using stacks. The queue methods
you need to implement are enqueue, dequeue, peek, and empty.
Step 1: Verify the constraints
- Do the queue methods we have to implement need to perform at the same complexity of a real queue?
: No, but they should be as performant as possible.
Step 2: Write out some test cases
Step 3: Figure out a solution without code
*/
// https://leetcode.com/problems/implement-queue-using-stacks/
type stackNode struct {
data int
next *stackNode
}
type MyQueue struct {
in *stackNode
out *stackNode
}
func NewMyQueue() MyQueue {
return MyQueue{}
}
// enqueue: append a value to the end of the queue
func (q *MyQueue) Push(x int) {
q.in = &stackNode{data: x, next: q.in}
}
// dequeue: remove the value at the start of the queue
func (q *MyQueue) Pop() int {
if q.out == nil {
q.move()
}
var result int
if q.out != nil {
result = q.out.data
q.out = q.out.next
}
return result
}
func (q *MyQueue) move() {
for q.in != nil {
tempNode := q.in
q.in = q.in.next
tempNode.next = q.out
q.out = tempNode
}
}
// peek: return the value at the start of the queue, not removing it
func (q *MyQueue) Peek() int {
if q.out == nil {
q.move()
}
var result int
if q.out != nil {
result = q.out.data
}
return result
}
// empty: return a boolean value of whether the queue is empty or not
func (q *MyQueue) Empty() bool {
return q.out == nil && q.in == nil
}