zoukankan      html  css  js  c++  java
  • 算法(Algorithms)第4版 练习 1.3.8

    方法实现:

    //1.3.8
    package com.qiusongde;
    
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    
    import edu.princeton.cs.algs4.StdIn;
    import edu.princeton.cs.algs4.StdOut;
    
    public class ResizingArrayStack<Item> implements Iterable<Item> {
    
        private Item[] content;
        private int number;
        
        public ResizingArrayStack() {
            content = (Item[]) new Object[1];
            number = 0;
        }
        
        private void resizeArray(int max) {
            
            if(max < number) 
                throw new IllegalArgumentException("the size of new array must larger than the size of Stack");
            
            Item[] temp = (Item[]) new Object[max];
            for(int i = 0; i < number; i++) {
                temp[i] = content[i];
            }
            content = temp;
        }
        
        
        public boolean isEmpty() {
            return number == 0;
        }
        
        public int size() {
            return number;
        }
        
        public void push(Item item) {
            
            if(number == content.length)
                resizeArray(2 * content.length);
            
            content[number++] = item;
        }
        
        public Item pop() {
            
            if(isEmpty())
                throw new NoSuchElementException("Stack is empty");
            
            Item item = content[--number];
            content[number] = null;//Aoid loitering
            
            if(number == content.length/4 && number > 0)
                resizeArray(content.length/2);
            
            return item;
        }
    
        @Override
        public Iterator<Item> iterator() {
            return new ReverseArrayIterator();
        }
        
        private class ReverseArrayIterator implements Iterator<Item> {
    
            private int i = number;
            
            @Override
            public boolean hasNext() {
                return i > 0;
            }
    
            @Override
            public Item next() {
                if(!hasNext())
                    throw new NoSuchElementException("Stack is empty");
                return content[--i];
            }
    
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
            
        }
        
        //Just for test(main)
        private int arrayLength() {
            return content.length;
        }
        
        public static void main(String[] args) {
            
            ResizingArrayStack<String> stack = new ResizingArrayStack<String>();
            StdOut.println("Initialized size:" + stack.size() + " Array Size:" + stack.arrayLength());
            
            while (!StdIn.isEmpty()) {
                
                String item = StdIn.readString();
                
                if (!item.equals("-")) {
                    
                    stack.push(item); 
                    StdOut.println("push success:" + item + " size:" + stack.size() + " Array Size:" + stack.arrayLength());
                    
                    StdOut.print("Left on stack: ");
                    for (String s : stack) {
                        StdOut.print(s + " ");
                    }
                    StdOut.println();
                    
                } else {
                    if(stack.isEmpty())
                        StdOut.println("pop error, stack empty");
                    else {
                        StdOut.println("pop success:" + stack.pop() + " size:" + stack.size() + " Array Size:" + stack.arrayLength());
                        
                        StdOut.print("Left on stack: ");
                        for (String s : stack) {
                            StdOut.print(s + " ");
                        }
                        StdOut.println();
                    }
                }
                
            }
            
        }
        
    }

    测试结果:

    Initialized size:0 Array Size:1
    it
    push success:it size:1 Array Size:1
    Left on stack: it 
    was
    push success:was size:2 Array Size:2
    Left on stack: was it 
    -
    pop success:was size:1 Array Size:2
    Left on stack: it 
    the
    push success:the size:2 Array Size:2
    Left on stack: the it 
    best
    push success:best size:3 Array Size:4
    Left on stack: best the it 
    -
    pop success:best size:2 Array Size:4
    Left on stack: the it 
    of
    push success:of size:3 Array Size:4
    Left on stack: of the it 
    times
    push success:times size:4 Array Size:4
    Left on stack: times of the it 
    -
    pop success:times size:3 Array Size:4
    Left on stack: of the it 
    -
    pop success:of size:2 Array Size:4
    Left on stack: the it 
    -
    pop success:the size:1 Array Size:2
    Left on stack: it 
    it
    push success:it size:2 Array Size:2
    Left on stack: it it 
    was
    push success:was size:3 Array Size:4
    Left on stack: was it it 
    -
    pop success:was size:2 Array Size:4
    Left on stack: it it 
    the
    push success:the size:3 Array Size:4
    Left on stack: the it it 
    -
    pop success:the size:2 Array Size:4
    Left on stack: it it 
    -
    pop success:it size:1 Array Size:2
    Left on stack: it
  • 相关阅读:
    handsontable-developer guide-data binding,data sources
    Android版Web服务器实现(三)HTTP响应
    Android版Web服务器实现(二)使用服务来监听HTTP请求
    HTTP协议详解(很详细)
    Android版Web服务器实现(一)HTTP协议请求头解析
    HTTP协议中Content-Length的详细解读。
    Android基于SwiFTP开源库的FTP实现(FTP匿名登录)
    Android之查看Wifi密码
    道德经与抽象、接口及框架
    JAVA中利用JNI与VS2012实现C/C++的DLL调用
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6513922.html
Copyright © 2011-2022 走看看