我该如何修复空堆栈异常?
how can i fix an empty stack exception?
我正在使用一个堆栈对另一个堆栈进行排序,方法是将所有节点从堆栈 1 转移到堆栈 2,只要 stack1.peek > stack2.peek。如果 stack1.peek 小于堆栈 2.peek,我将所有大于 stack1.peek 的节点移动到堆栈 1.
在 else if 块中,当堆栈 2 暂时变空时出现问题。我如何编写我的代码以忽略此异常?我需要循环来保持 运行 即使堆栈 2 暂时为空。
这是代码:
import java.util.*;
public class SortedStack {
/*
* Method
*
* Stack 1 is the original stack
* Stack 2 is the helper stack
*/
public static void sortStack(Stack<Integer> stack1) {
// second, helper stack:
Stack<Integer> stack2 = new Stack<Integer>();
int count = 0;
// loop through each node in stack 2, compare to current node at top of stack 1
while (!stack1.isEmpty()) { // loop until entire stack 1 is sorted
int temp1 = stack1.pop();
// 1. STACK 2 IS EMPTY
if (stack2.isEmpty()) { // if stack 2 is empty and we're at the beginning of the problem
stack2.push(temp1);
}
// IF STACK 1 NODE < STACK 2 NODE
else if (temp1 < stack2.peek()) {
// If the S1 node is smaller than the top S2 node, we need to rearrange things.
// All nodes in S2 that are bigger than S1 temp are transferred to S1, and then added back once S1 temp is pushed into S2
while (temp1 < stack2.peek()) {
int temp2 = stack2.pop();
stack1.push(temp2);
count++;
}
// add top node of S1 to stack 2
stack2.push(temp1);
// add these nodes back to stack 2
while (count >0) {
int temp3 = stack1.pop();
stack2.push(temp3);
count--;
}
}
// IF STACK 1 NODE > STACK 2 NODE
else { // (temp1 > stack2.peek())
stack2.push(temp1); // if the S1 node is bigger than the S2 top node, we just add the S1 node over to S2
}
}
System.out.println(stack2.toString());
}
// Run the method
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.add(34);
stack.add(3);
stack.add(31);
stack.add(98);
stack.add(92);
stack.add(23);
sortStack(stack);
}
}
谢谢!
所以,我只需要在 else 中添加这一行 if:
while (!stack2.isEmpty() && temp1 < stack2.peek()) {
int temp2 = stack2.pop();
stack1.push(temp2);
count++;
}
我的错!留下这个以防将来有人尝试这个问题并遇到这个问题。这是“仅使用临时堆栈对堆栈进行排序”
我正在使用一个堆栈对另一个堆栈进行排序,方法是将所有节点从堆栈 1 转移到堆栈 2,只要 stack1.peek > stack2.peek。如果 stack1.peek 小于堆栈 2.peek,我将所有大于 stack1.peek 的节点移动到堆栈 1.
在 else if 块中,当堆栈 2 暂时变空时出现问题。我如何编写我的代码以忽略此异常?我需要循环来保持 运行 即使堆栈 2 暂时为空。
这是代码:
import java.util.*;
public class SortedStack {
/*
* Method
*
* Stack 1 is the original stack
* Stack 2 is the helper stack
*/
public static void sortStack(Stack<Integer> stack1) {
// second, helper stack:
Stack<Integer> stack2 = new Stack<Integer>();
int count = 0;
// loop through each node in stack 2, compare to current node at top of stack 1
while (!stack1.isEmpty()) { // loop until entire stack 1 is sorted
int temp1 = stack1.pop();
// 1. STACK 2 IS EMPTY
if (stack2.isEmpty()) { // if stack 2 is empty and we're at the beginning of the problem
stack2.push(temp1);
}
// IF STACK 1 NODE < STACK 2 NODE
else if (temp1 < stack2.peek()) {
// If the S1 node is smaller than the top S2 node, we need to rearrange things.
// All nodes in S2 that are bigger than S1 temp are transferred to S1, and then added back once S1 temp is pushed into S2
while (temp1 < stack2.peek()) {
int temp2 = stack2.pop();
stack1.push(temp2);
count++;
}
// add top node of S1 to stack 2
stack2.push(temp1);
// add these nodes back to stack 2
while (count >0) {
int temp3 = stack1.pop();
stack2.push(temp3);
count--;
}
}
// IF STACK 1 NODE > STACK 2 NODE
else { // (temp1 > stack2.peek())
stack2.push(temp1); // if the S1 node is bigger than the S2 top node, we just add the S1 node over to S2
}
}
System.out.println(stack2.toString());
}
// Run the method
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.add(34);
stack.add(3);
stack.add(31);
stack.add(98);
stack.add(92);
stack.add(23);
sortStack(stack);
}
}
谢谢!
所以,我只需要在 else 中添加这一行 if:
while (!stack2.isEmpty() && temp1 < stack2.peek()) {
int temp2 = stack2.pop();
stack1.push(temp2);
count++;
}
我的错!留下这个以防将来有人尝试这个问题并遇到这个问题。这是“仅使用临时堆栈对堆栈进行排序”