算法第四版1.3背包、队列和栈:习题1.3.48

import edu.princeton.cs.algs4.StdOut;

public class E1_3_48 {
    public static void main(String[]args){
        E1_3_48 twoStackWithDeque=new E1_3_48<>();
        for (int i=0;i<5;i++)//将0-4压入左栈
            twoStackWithDeque.pushLeftStack(i);
        for (int i=5;i<10;i++)//将5-9压入右栈
            twoStackWithDeque.pushRightStack(i);
        for (int i:twoStackWithDeque.deque)//遍历两个栈,结果应该为4,3,2,1,0,5,6,7,8,9
            StdOut.print(i+" ");
        StdOut.println();
        int count=twoStackWithDeque.LeftStackSize();
        for (int i=0;i deque=new Deque<>();
    private int LeftCount;
    private int RightCount;
    private class Node{
        Item item;
        Node next;
    }
    public boolean LeftStackIsEmpty(){return LeftCount==0;}
    public boolean RightStackIsEmpty(){return RightCount==0;}
    public int LeftStackSize(){return LeftCount;}
    public int RightStackSize(){return RightCount;}
    public void pushLeftStack(Item item){
        deque.pushLeft(item);
        LeftCount++;
    }
    public void pushRightStack(Item item){
        deque.pushRight(item);
        RightCount++;
    }
    public Item popLeftStack(){
        if (LeftStackIsEmpty())return null;
        LeftCount--;
        return  deque.popLeft();
    }
    public Item popRightStack(){
        if (RightStackIsEmpty())return null;
        RightCount--;
        return  deque.popRight();
    }
}

 

你可能感兴趣的:(算法第四版1.3背包,队列和栈课后习题)