Java数据结构之——栈:用数组实现


/**
* This is an abstract data type interface for the stack. 
* This interface includes methods: 
* {@code pop},{@code push},{@code peek},{@code isEmpty},{@code size}.
*
* @excetpion EmptyCollectionException
*   Indicates that the collection is empty.
* @author 
*   e-mail: 
*   
**/
interface StackADT<T>
{
	public void push(T element);
	public T pop() throws EmptyCollectionException;
	public T peek() throws EmptyCollectionException;
	public Boolean isEmpty();
	public int size();
}


class EmptyCollectionException extends Exception{
    public EmptyCollectionException(){
        super();
    }
    public EmptyCollectionException(String mess){
        super(mess);
    }
}
/**
* This class implements the interface {@code StackADT}. 
*
* @author Yudong Pu
*   e-mail: [email protected]
*   Stony Brook ID: 111278075
**/
public class BlockStack<T> implements StackADT<T>
{
	private final int DEFAULT_CAPACITY = 100;
	private int top;
	private T[] stack;
	
    /**
    * Constructs a new empty {@code BlockStack} object 
    * without any parameters.
    * The initial capacity is {@code DEFAULT_CAPACITY}.
    **/
	@SuppressWarnings("unchecked")
	public BlockStack()
	{
		top = 0;
		stack = (T[]) (new Object[DEFAULT_CAPACITY]);
	}
    
    /**
    * Constructs a new empty {@code BlockStack} object 
    * without any parameters.
    *
    * @param initialCapacity Determined thi initial capacity of the stack.       
    **/
	@SuppressWarnings("unchecked")
	public BlockStack(int initialCapacity)
	{
		top = 0;
		stack = (T[]) (new Object[initialCapacity]);
	}
	
    /**
     * Pushes an item onto the top of this stack. 
     *
     * @param   element   the item to be pushed onto this stack.
     */
	public void push(T element)
	{
		if (size() == stack.length)
		{
			expandCapacity();
		}
		stack[top] = element;
		top++;
	}
	
    /**
     * Expands the capacity of this stack.
     *
     */
	@SuppressWarnings("unchecked")
	private void expandCapacity()
	{
		T[] larger = (T[]) (new Object[stack.length*2]);
		for(int index = 0; index < stack.length; index++)
		{
			larger[index] = stack[index];
		}
		stack = larger;
	}
	
    /**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack.
     * @throws  EmptyCollectionException  if this stack is empty.
     */
	public T pop() throws EmptyCollectionException
	{
		if(isEmpty())
		{
			throw new EmptyCollectionException("stack is empty.");
		}
		top--;
		T result = stack[top];
		stack[top] = null;
		return result;
	}
	
    /**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack.
     * @throws  EmptyCollectionException  if this stack is empty.
     */
	public T peek() throws EmptyCollectionException
	{
		if(isEmpty())
		{
			throw new EmptyCollectionException("stack");
		}
		return stack[top-1];
	}
	
    
    /**
     * Tests if this stack is empty.
     *
     * @return  {@code true} if and only if this stack contains
     *          no items; {@code false} otherwise.
     */
	public Boolean isEmpty()
	{
		Boolean bool = false;
		if(top == 0)
		{
			bool = true;
		}
		return bool;
	}
	
    /**
     * Gets the size of this stack.
     *
     * @return  The location of the object at the top of this stack. 
     */
	public int size()
	{
		return top;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_26552071/article/details/86224264