简体   繁体   English

如何修复空堆栈异常?

[英]how can i fix an empty stack exception?

I'm using a stack to sort another stack, by transferring all nodes from stack 1 to stack 2 as long as stack1.peek > stack2.peek.我正在使用堆栈对另一个堆栈进行排序,只要 stack1.peek > stack2.peek 将所有节点从堆栈 1 转移到堆栈 2。 If stack1.peek is less than stack 2. peek, I move all nodes that are more than stack1.peek over to stack 1.如果 stack1.peek 小于 stack 2.peek,我将所有大于 stack1.peek 的节点移动到 stack 1。

On the else if block there's an issue when stack 2 becomes temporarily empty.在 else if 块上,当堆栈 2 暂时为空时会出现问题。 How can I program my code to ignore this exception?如何编写代码以忽略此异常? i need the loop to keep running even is stack 2 is momentarily empty.我需要循环继续运行,即使堆栈 2 暂时为空。

This is the code:这是代码:

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);
}

}

Thank you!谢谢!

So, I just needed to add this line inside the else if:所以,我只需要在 else if 中添加这一行:

                while (!stack2.isEmpty() && temp1 < stack2.peek()) {
                    int temp2 = stack2.pop();
                    stack1.push(temp2);
                    count++;
                }

My bad.我的错。 Leaving this in case anyone in the future tries this problem and runs into this.留下这个,以防将来有人尝试这个问题并遇到这个问题。 It's the "sort a stack using only a temporary stack"这是“仅使用临时堆栈对堆栈进行排序”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM