forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingStacks.java
More file actions
47 lines (40 loc) · 976 Bytes
/
Copy pathQueueUsingStacks.java
File metadata and controls
47 lines (40 loc) · 976 Bytes
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
/**
* Using inverted arrays to track the stack and its correspinding values.
*/
class MyQueue {
private Integer[] input;
private Integer[] output;
int inputSize;
int outputSize;
int n = 1000;
public MyQueue() {
input = new Integer[n];
output = new Integer[n];
inputSize = 0;
outputSize = 0;
}
public void push(int x) {
input[inputSize] = x;
inputSize++;
}
public int pop() {
int val = peek();
output[outputSize] = null;
outputSize--;
return val;
}
public int peek() {
if(outputSize == 0){
while(inputSize > 0){
output[inputSize] = input[outputSize];
input[outputSize] = null;
outputSize++;
inputSize--;
}
}
return output[outputSize];
}
public boolean empty() {
return outputSize == 0 && inputSize == 0;
}
}