-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakeQueueWithTwoStacks.java
More file actions
68 lines (59 loc) · 1.86 KB
/
MakeQueueWithTwoStacks.java
File metadata and controls
68 lines (59 loc) · 1.86 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
package com.codefortomorrow.advanced.chapter17.solutions;
import java.util.NoSuchElementException;
import java.util.Stack;
/*
This implementation of MyQueue uses the first stack as LIFO order "storage" space.
The second stack is is always in FIFO order.
When the user adds into the MyQueue, the operation is instant. When the user removes from the MyQueue,
3 things could occur:
1. there is nothing in the MyQueue: return null
2. stack2 is empty, so copy over everything from stack1: return the top of stack2
3. return the top of stack2
*/
class MyQueue<E> {
Stack<E> stack1;
Stack<E> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void add(E e) {
stack1.push(e);
}
public E remove() throws NoSuchElementException {
if (empty()) throw new NoSuchElementException(
"no element in this queue left to remove"
); else if (stack2.empty()) {
while (!stack1.empty()) stack2.push(stack1.pop());
return stack2.pop();
} else {
return stack2.pop();
}
}
public boolean empty() {
return stack1.empty() && stack2.empty();
}
}
public class MakeQueueWithTwoStacks {
public static void main(String[] args) {
MyQueue<Integer> myQueue = new MyQueue<>();
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");
}
}