-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (28 loc) · 716 Bytes
/
Solution.java
File metadata and controls
34 lines (28 loc) · 716 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
import java.util.*;
public class Solution {
private class NewQueue<T>(){
private Stack<T> stackNew = new Stack<T>();
private Stack<T> stackOld = new Stack<T>();
public void enqueue(T value){
//add item in the stackNew
stackNew.push(value);
}
public T peek(){
//move elements from stackNew to stackOld
moveStacks();
return stackOld.peek();
}
private void moveStacks(){
if(stackold.isEmpty()){
while(!stackNew.isEmpty()){
stackOld.push(stackNew.pop());
}
}
}
public T dequeue(){
//move elements from stackNew to stackOld
moveStacks();
return stackOld.pop();
}
}
}