-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxStack.java
More file actions
30 lines (24 loc) · 973 Bytes
/
MaxStack.java
File metadata and controls
30 lines (24 loc) · 973 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
/**
* A stacklike data structure that also allows stacklike access
* to its elements by their value.
* For example, given a stack of {1, 3, 2, 5, 3, 4, 5, 2}
* peek() -> 2, peekMax() -> 5
* pop() -> 2; peek() -> 5, peekMax() -> 5
* pop() -> 5; peek() -> 4, peekMax() -> 5
* push(6); peek() -> 6, peekMax() -> 6
* popMax() -> 6; peek -> 4, peekMax() -> 5
* popMax() -> 5; peek -> 4, peekMax() -> 4
*/
public interface MaxStack<T extends Comparable<T>> {
/** Add an element to the stack. */
public void push(T toPush);
/** Return the top value on the stack. */
public T peek();
/** Remove and return the top value from the stack. */
public T pop();
// Two special methods, so this isn't just 'implement a stack':
/** Return the largest value in the stack. (Remember that T must implement Comparable.) */
public T peekMax();
/** Remove and return the largest value from the stack. */
public T popMax();
}