ArrayStack -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //******************************************************************** // ArrayStack.java Authors: Lewis/Chase // // Represents an array implementation of a stack. //******************************************************************** package jss2; import jss2.exceptions.*; import java.util.Iterator; public class ArrayStack<T> implements StackADT<T> { private final int DEFAULT_CAPACITY = 100; private int top; // indicates the next open slot private transient T stack; //----------------------------------------------------------------- // Creates an empty stack using the default capacity. //----------------------------------------------------------------- public ArrayStack() { top = 0; stack = (T)(new Object[DEFAULT_CAPACITY]); } //----------------------------------------------------------------- // Creates an empty stack using the specified capacity. //----------------------------------------------------------------- public ArrayStack (int initialCapacity) { top = 0; stack = (T)(new Object[initialCapacity]); } //----------------------------------------------------------------- // Adds the specified element to the top of the stack, expanding // the capacity of the stack array if necessary. //----------------------------------------------------------------- public void push (T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } //----------------------------------------------------------------- // Removes the element at the top of the stack and returns a // reference to it. Throws an EmptyStackException if the stack // is empty. //----------------------------------------------------------------- public T pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); top--; T result = stack[top]; stack[top] = null; return result; } //----------------------------------------------------------------- // Returns a reference to the element at the top of the stack. // The element is not removed from the stack. Throws an // EmptyStackException if the stack is empty....
View Full Document

This note was uploaded on 09/20/2010 for the course COMPUTER S 1604 taught by Professor Zhu during the Fall '08 term at University of Texas Southwestern Medical Center at Dallas.

Page1 / 10

ArrayStack -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online