• 下压桟(LIFO)


      

      此代码并不复杂,但却是其他集合数据类型的实现的模版

    import java.util.Iterator;
    
    public class ResizingArrayStackStack<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;
        }
    
        public void push(Item item){
            if(N == a.length)resize(2*a.length);
            a[N++] = item;
        }
    
        public Item pop(){
            Item item = a[--N];
            a[N] = null;
            if(N > 0 && N == a.length/4) resize(a.length/2);
            return item;
        }
    
        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()    {}
        }
    }
  • 相关阅读:
    效能分析
    个人项目词频统计2
    PSP 进度条 柱状图 饼状图
    四人小组项目
    结对编程
    品评历届学生作品
    构建之法终于到手了
    补上周一的进度条进度条:
    燃尽图
    scrum站立会议
  • 原文地址:https://www.cnblogs.com/haoyangblog/p/7586669.html
Copyright © 2020-2023  润新知