-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (34 loc) · 785 Bytes
/
Solution.java
File metadata and controls
39 lines (34 loc) · 785 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
package stack;
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if ( stack2.empty() ) {
while ( !stack1.empty() ) {
int temp = stack1.pop();
stack2.push(temp);
}
}
return stack2.pop();
}
public static void main(String[] args) {
int[] testArr = { 8, 4, 3, 6, 7, 3, 2, 1, 9, 4, 3 };
Solution ss = new Solution();
for (int i = 0; i < 4; i++) {
ss.push(testArr[i]);
}
for (int i = 0; i < 4; i++) {
System.out.println("**" + ss.pop());
}
for (int i = 4; i < 8; i++) {
ss.push(testArr[i]);
}
for (int i = 4; i < 8; i++) {
System.out.println("**" + ss.pop());
}
}
}