算法(Algorithms)第4版 练习 1.3.8

Posted 我是老邱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法(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

 

以上是关于算法(Algorithms)第4版 练习 1.3.8的主要内容,如果未能解决你的问题,请参考以下文章

算法(Algorithms)第4版 练习 1.3.23 1.3.22

算法(Algorithms)第4版 练习 1.3.26

算法(Algorithms)第4版 练习 1.3.31

算法(Algorithms)第4版 练习 1.3.29

算法(Algorithms)第4版 练习 1.3.14

算法(Algorithms)第4版 练习 1.3.8