-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay20.java
More file actions
41 lines (33 loc) · 1.01 KB
/
Day20.java
File metadata and controls
41 lines (33 loc) · 1.01 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
import java.util.NoSuchElementException;
import java.util.Stack;
public class Day20 {
private Stack<Integer> stack1 = new Stack<>();
private Stack<Integer> stack2 = new Stack<>();
public void enqueue(int item) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack1.push(item);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
public int dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
// Pop the front element from stack1
return stack1.pop();
}
public boolean isEmpty() {
return stack1.isEmpty();
}
public static void main(String[] args) {
Day20 queue = new Day20();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Dequeue: " + queue.dequeue());
System.out.println("Dequeue: " + queue.dequeue());
}
}