Algs4-1.3.1为FixedCapacityStackOfStrings添加方法isFull

 1.3.1为FixedCapacityStackOfStrings添加一个方法isFull()
 public boolean isFull()
    {return N==a.length;}

完整Code:
public class FixedCapacityStackOfStrings
{
    private String[] a;
    private int N;
    public FixedCapacityStackOfStrings(int cap)
    {a=new String[cap];}
   
    public void push(String item)
    {a[N++]=item;}
   
    public String pop()
    {return a[--N];}
   
    public boolean isEmpty()
    {return N==0;}
   
    public int size()
    {return N;}
   
    public boolean isFull()
    {return N==a.length;}
   
    public static void main(String[] args)
    {
        FixedCapacityStackOfStrings s;
        s=new FixedCapacityStackOfStrings(100);
        while (!StdIn.isEmpty())
        {
            String item=StdIn.readString();
            if(!item.equals("-"))
                s.push(item);
            else if(!s.isEmpty())
                StdOut.printf(s.pop()+" ");
        }//end while
        StdOut.println("("+s.size()+" left on stack)");
    }//end main
}//end class

posted @ 2018-10-25 13:42  修电脑的龙生  阅读(273)  评论(0编辑  收藏  举报