-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakeQueueWithOneStack.java
More file actions
69 lines (58 loc) · 1.91 KB
/
MakeQueueWithOneStack.java
File metadata and controls
69 lines (58 loc) · 1.91 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
package com.codefortomorrow.advanced.chapter17.solutions;
import java.util.NoSuchElementException;
import java.util.Stack;
/*
This implementation of MyQueueV2 uses the first stack as LIFO order "storage" space.
In order to get the functionality of another stack, we will use recursion
When the user adds into the MyQueueV2, the operation is instant. When the user removes from the MyQueueV2,
the remove method either throws an exception, or recursively pops from the stack until the first element inserted
is isolated. After the first element is returned, the stack of Es is restored as the recursive call stack shrinks
*/
class MyQueueV2<E> {
Stack<E> stack;
public MyQueueV2() {
stack = new Stack<>();
}
public void add(E e) {
stack.push(e);
}
public E remove() throws NoSuchElementException {
if (empty()) throw new NoSuchElementException(
"no element in this queue left to remove"
);
return removeHelper();
}
private E removeHelper() {
if (stack.size() == 1) return stack.pop();
E e = stack.pop();
E val = removeHelper();
stack.push(e);
return val;
}
public boolean empty() {
return stack.empty();
}
}
public class MakeQueueWithOneStack {
public static void main(String[] args) {
MyQueueV2<Integer> myQueue = new MyQueueV2<>();
myQueue.add(1);
Integer a = myQueue.remove();
System.out.println(a); // 1
myQueue.add(2);
myQueue.add(3);
myQueue.add(4);
a = myQueue.remove();
System.out.println(a); // 2
a = myQueue.remove();
System.out.println(a); // 3
a = myQueue.remove();
System.out.println(a); // 4
try {
a = myQueue.remove();
} catch (NoSuchElementException e) {
e.printStackTrace();
}
System.out.println("tests complete");
}
}