-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathStack.java
More file actions
34 lines (26 loc) · 718 Bytes
/
Stack.java
File metadata and controls
34 lines (26 loc) · 718 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
package ssj.algorithm;
import ssj.algorithm.collections.LinkedStack;
import java.util.Comparator;
/**
* Created by shenshijun on 15/2/4.
*/
public interface Stack<T> {
void push(T ele);
T pop();
int size();
public default boolean isEmpty() {
return size() <= 0;
}
T head();
public default Stack<T> sortStack(Comparator<? super T> comparator) {
LinkedStack<T> result = new LinkedStack<>();
while (!isEmpty()) {
T cur_ele = pop();
while (!result.isEmpty() && comparator.compare(result.head(), cur_ele) > 0) {
push(result.pop());
}
result.push(cur_ele);
}
return result;
}
}