public class ArrayStack<E> extends java.lang.Object implements Stack<E>
//begin#fragment ArrayStack
Modifier and Type | Field and Description |
---|---|
static int |
CAPACITY
Default array capacity.
|
Constructor and Description |
---|
ArrayStack()
Initializes the stack to use an array of default length.
|
ArrayStack(int cap)
Initializes the stack to use an array of given length.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Testes whether the stack is empty.
|
static void |
main(java.lang.String[] args)
Test our program by performing a series of operations on stacks,
printing the operations performed, the returned elements and the
contents of the stack involved, after each operation.
|
E |
pop()
Removes the top element from the stack.
|
void |
push(E element)
Inserts an element at the top of the stack.
|
int |
size()
Returns the number of elements in the stack.
|
void |
status(java.lang.String op,
java.lang.Object element)
Prints status information about a recent operation and the stack.
|
E |
top()
Inspects the element at the top of the stack.
|
java.lang.String |
toString()
Returns a string representation of the stack as a list of elements,
with the top element at the end: [ ...
|
public static final int CAPACITY
public ArrayStack()
public ArrayStack(int cap)
cap
- length of the array.public boolean isEmpty()
public static void main(java.lang.String[] args)
public E pop() throws EmptyStackException
pop
in interface Stack<E>
EmptyStackException
- if the stack is empty.public void push(E element) throws FullStackException
push
in interface Stack<E>
element
- element to be inserted.FullStackException
- if the array storing the elements is full.public int size()
public void status(java.lang.String op, java.lang.Object element)
op
- operation performedelement
- element returned by the operationpublic E top() throws EmptyStackException
top
in interface Stack<E>
EmptyStackException
- if the stack is empty.public java.lang.String toString()
toString
in class java.lang.Object