双端队列实现栈

4.3 编写一个基于上机作业4.2的Deque类的栈类。这个栈类应该与 stack.java程序(清单4.1)中的StackX类具有机同的方法和功能。


public class StackY {
    private DuQueue stackQueue;

    public StackY(int size){
        stackQueue = new DuQueue(size);
    }

    public void push(long value){
        stackQueue.insertRight(value);
    }

    public long pop(){
        return stackQueue.removeRight();
    }

    public long peek(){
        return stackQueue.peekRight();
    }

    public boolean isEmpty(){
        return stackQueue.isEmpty();
    }

    public boolean isFull(){
        return stackQueue.isFull();
    }

}

public class StackYApp {

    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StackY theStack = new StackY(5);
        System.out.println("Stack is Empty : " + theStack.isEmpty());
        System.out.println("Stack is Full : " + theStack.isFull());
        theStack.push(20);
        theStack.push(40);
        theStack.push(60);
        theStack.push(80);
        theStack.push(90);
        System.out.println("Stack is Empty : " + theStack.isEmpty());
        System.out.println("Stack is Full : " + theStack.isFull());
        while(!theStack.isEmpty()){
            long value = theStack.pop();
            System.out.print(value);
            System.out.print(" ");
        }
    }
}

你可能感兴趣的:(队列,栈)