-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
StackOfPlates.java
80 lines (71 loc) · 2.32 KB
/
StackOfPlates.java
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
70
71
72
73
74
75
76
77
78
79
80
package com.ctci.stacksandqueues;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Stack;
/**
* Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we
* would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure
* SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once
* the previous one exceeds capacity. SetOfStacks.push() and SetOfStacks. pop() should behave identically to a single
* stack (that is, pop() should return the same values as it would if there were just a single stack).
*
* @author rampatra
* @since 2019-02-08
*/
public class StackOfPlates {
private static final int capacity = 3;
private static List<Stack<Integer>> stackList = new ArrayList<>();
private static int push(int item) {
return getLastStack().push(item);
}
private static int pop() {
Stack<Integer> lastStack = stackList.get(stackList.size() - 1);
if (lastStack == null || (stackList.size() == 1 && lastStack.empty())) {
throw new EmptyStackException();
} else if (lastStack.empty()) {
stackList.remove(stackList.size() - 1);
return pop();
} else {
return lastStack.pop();
}
}
private static Stack<Integer> getLastStack() {
if (stackList.size() == 0 || isFull(stackList.get(stackList.size() - 1))) {
stackList.add(new Stack<>());
}
return stackList.get(stackList.size() - 1);
}
private static boolean isFull(Stack<Integer> stack) {
return stack.size() >= capacity;
}
private static void print() {
System.out.print("[");
stackList.stream().flatMap(Collection::stream).forEach(System.out::print);
System.out.println("]");
}
public static void main(String[] args) {
push(1);
push(2);
print();
push(3);
print();
push(4);
push(5);
print();
push(6);
push(7);
print();
pop();
print();
pop();
pop();
pop();
print();
pop();
pop();
pop();
print();
}
}