• Algs4-1.3不定容数组实现泛型栈支持迭代


    import java.util.Iterator;
    public class ResizingArrayStack<Item> implements Iterable<Item>
    {
        private Item[] a=(Item[]) new Object[1];
        private int N=0;
        public boolean isEmpty(){return N==0;}
        public int size(){return N;}
        private void resize(int max)
        {
            Item[] temp=(Item[]) new Object[max];
            for(int i=0;i<N;i++)
                temp[i]=a[i];
            a=temp;
        }//end resize
        public void push(Item item)
        {
            if(N==a.length) resize(2*a.length);
                a[N++]=item;
        }//end push
        public Item pop()
        {
            Item item=a[--N];
            a[N]=null;
            if(N>0 && N==a.length/4) resize(a.length/2);
            return item;
        }//end pop
        public Iterator<Item> iterator()
        {return new ReverseArrayIterator();}
       
        private class ReverseArrayIterator implements Iterator<Item>
        {
            private int i=N;
            public boolean hasNext(){return i>0;}
            public Item next(){return a[--i];}
            public void remove(){}
         }
       
        public static void main(String[] args)
        {
            ResizingArrayStack<String> s;
            s=new ResizingArrayStack();
            while(!StdIn.isEmpty())
            {
                String item=StdIn.readString();
                if(!item.equals("-"))
                    s.push(item);
             }//end while
            for(String i:s)
                StdOut.printf(i+" ");
        }
    }

  • 相关阅读:
    区块链技术术语表
    以太坊客户端Geth命令用法-参数详解
    智能合约开发环境搭建及Hello World合约
    以太坊是什么
    比特币区块结构Merkle树及简单支付验证分析
    非对称加密技术- RSA算法数学原理分析
    验证APNS证书的有效性
    十八般武艺之 Runloop
    iOS 所有设备一览 && CoreFoundation源码
    [User Defaults] Failed to read values in CFPrefsPlistSource (iOS 10)
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849260.html
Copyright © 2020-2023  润新知