【问题标题】:Creating queue from two stacks从两个堆栈创建队列
【发布时间】:2013-07-24 04:35:15
【问题描述】:

有人可以解释我在这里做错了什么吗? 我正在尝试根据书籍练习从两个堆栈创建一个队列。我从 peek 函数中收到错误“堆栈下溢”。但对我来说一切似乎都是正确的:P 请解释一下。谢谢!

//Program to implement Queue using two Stacks.

import java.util.NoSuchElementException;

public class Ex3_5_Stack {
    int N;
    int countOfNodes=0;
    private Node first;

    class Node {
        private int item;
        private Node next;
    }

    public Ex3_5_Stack() {
        first=null;
        N=0;
    }

    public int size() {
        return N;
    }

    public boolean isEmpty() {
        return first==null;
    }

    public void push(int item){
        if (this.countOfNodes>=3) {
            Ex3_5_Stack stack = new Ex3_5_Stack();
            stack.first.item=item;
            N++;
        } else {
            Node oldfirst = first;
            first = new Node();
            first.item=item;
            first.next=oldfirst;
            N++;
        }
    }

    public int pop() {
        if (this.isEmpty()) 
            throw new NoSuchElementException("Stack Underflow");

        int item = first.item;
        first=first.next;
        return item;
    }

    public int peek() {
        if (this.isEmpty()) 
            throw new NoSuchElementException("Stack Underflow");

        return first.item;
    }
}

还有我的队列文件

public class Ex3_5_MyQueue {
    Ex3_5_Stack StackNewest,StackOldest;

    public Ex3_5_MyQueue() {
        super();
        StackNewest = new Ex3_5_Stack();
        StackOldest = new Ex3_5_Stack();
    }

    public int size() {
        return StackNewest.size()+StackOldest.size();
    }

    public void add(int value) {
        StackNewest.push(value);
    }

    private void transferStack() {
        if (StackOldest.isEmpty()) {
            while (StackNewest.isEmpty()) {
                StackOldest.push(StackNewest.pop());
            }
        }
    }

    public int peek() {
        this.transferStack();
        return StackOldest.peek();
    }

    public int remove() {
        this.transferStack();
        return StackOldest.pop();
    }

    public static void main(String[] args) {
        Ex3_5_MyQueue myQueue = new Ex3_5_MyQueue();
        myQueue.add(4);
        myQueue.add(3);
        myQueue.add(5);
        myQueue.add(1);
        System.out.println(myQueue.peek());
    }
}

【问题讨论】:

    标签: java stack queue


    【解决方案1】:

    transferStack() 中,您缺少一个感叹号。应该是:

    private void transferStack(){
        if(StackOldest.isEmpty()){
            while(!StackNewest.isEmpty()){ // you forgot it here
                StackOldest.push(StackNewest.pop());
            }
        }
    }
    

    【讨论】:

    • 另外,为什么你的堆栈push() 代码中有那个if (this.countOfNodes>=3) 子句?您正在创建一个全新的堆栈,将物品推入其中,然后您甚至不保存该新堆栈,因此您所拥有的只是旧物品,即使您的计数增加了 1。
    • 谢谢!那是另一个相关程序。与上述目标无关。
    • 如果你这么说,尽管我希望它的用途有据可查,以便知道它为什么这样做。
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2011-11-29
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2018-06-14
    相关资源
    最近更新 更多