-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxElemStack.java
More file actions
43 lines (36 loc) · 940 Bytes
/
Copy pathMaxElemStack.java
File metadata and controls
43 lines (36 loc) · 940 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
43
package Stack_Queue;
import java.util.Stack;
public class MaxElemStack {
private final Stack<Integer> stack;
private final Stack<Integer> auxiliaryStack;
public MaxElemStack(){
stack = new Stack<>();
auxiliaryStack = new Stack<>();
}
public void push(int x){
if (stack.empty()){
auxiliaryStack.push(x);
}else{
if (x > auxiliaryStack.peek()){
auxiliaryStack.push(x);
}else {
auxiliaryStack.push(auxiliaryStack.peek());
}
}
stack.push(x);
}
public int pop(){
if (size() > 0) {
stack.pop();
return auxiliaryStack.pop();
}else {
return -1;
}
}
public int peekMax(){
return auxiliaryStack.peek();
}
public int size(){
return stack.size();
}
}