-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingQueue.java
More file actions
42 lines (35 loc) · 1023 Bytes
/
StackUsingQueue.java
File metadata and controls
42 lines (35 loc) · 1023 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
package Stack;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.stream.IntStream;
public class StackUsingQueue {
public static void main(String[] args) {
StackImplementation<Integer> stackImplementation = new StackImplementation<Integer>();
IntStream.range(0, 10).forEach(stackImplementation::addToStack);
int n = stackImplementation.getStackSize();
for(int i = 0; i < n; i++) {
System.out.print(stackImplementation.getFromStack());
System.out.print(" ");
}
}
}
class StackImplementation<E> {
Deque<E> deque;
StackImplementation() {
deque = new ArrayDeque<E>();
}
public void addToStack(E e) {
deque.offerFirst(e);
}
public int getStackSize() {
return deque.size();
}
public E getFromStack() {
int n = deque.size();
while(n-- > 0) {
deque.offerFirst(deque.remove());
//n--;
}
return deque.remove();
}
}