-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMain.java
More file actions
69 lines (53 loc) · 1.62 KB
/
Copy pathTestMain.java
File metadata and controls
69 lines (53 loc) · 1.62 KB
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
import java.util.*;
public class TestMain {
public static void main(String[] args) {
// Map<String, String> map = new TreeMap();
// map.put("Hello", "World");
// map.put("cool", "beans");
// Map<String, String> newMap = reverse(map);
// System.out.println(newMap);
Stack<Integer> stack = new Stack<>();
stack.push(3);
stack.push(5);
stack.push(40);
stack.push(10);
stack.push(3);
stack.push(40);
// System.out.println(stack);
//
// System.out.println("max: " + removeMax(stack));
// System.out.println(stack);
LinkedList
}
public static <V, K> Map<V, K> reverse(Map<K, V> map) {
Map<V, K> newMap = new HashMap<>();
for (K key : map.keySet()) {
newMap.put(map.get(key), key);
}
return newMap;
}
public static int removeMax(Stack<Integer> stack) {
Queue<Integer> queue = new LinkedList<>();
int max = Integer.MIN_VALUE;
// find max in stack while adding each value to queue
while (!stack.isEmpty()) {
int num = stack.pop();
if (num > max)
max = num;
queue.add(num);
}
// readd all values from queue except max to stack
while (!queue.isEmpty()) {
int num = queue.remove();
if (num != max)
stack.push(num);
}
// readd values to queue and then stack to revert
// back to original order
while (!stack.isEmpty())
queue.add(stack.pop());
while (!queue.isEmpty())
stack.push(queue.remove());
return max;
}
}