Package com.zybooks.dsaj.stackqueue
Class ArrayStack<E>
java.lang.Object
com.zybooks.dsaj.stackqueue.ArrayStack<E>
- Type Parameters:
E
- the element type
- All Implemented Interfaces:
Stack<E>
Implementation of the stack ADT using a fixed-length array. All
operations are performed in constant time. An exception is thrown
if a push operation is attempted when the size of the stack is
equal to the length of the array.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs an empty stack using the default array capacity.ArrayStack
(int capacity) Constructs and empty stack with the given array capacity. -
Method Summary
Modifier and TypeMethodDescriptionboolean
isEmpty()
Tests whether the stack is empty.static void
Demonstrates sample usage of a stack.pop()
Removes and returns the top element from the stack.void
Adds an element at the top of the stack.int
size()
Returns the number of elements in the stack.top()
Returns, but does not remove, the element at the top of the stack.toString()
Produces a string representation of the contents of the stack.
-
Field Details
-
CAPACITY
public static final int CAPACITYDefault array capacity.- See Also:
-
-
Constructor Details
-
ArrayStack
public ArrayStack()Constructs an empty stack using the default array capacity. -
ArrayStack
public ArrayStack(int capacity) Constructs and empty stack with the given array capacity.- Parameters:
capacity
- length of the underlying array
-
-
Method Details
-
size
public int size()Returns the number of elements in the stack. -
isEmpty
public boolean isEmpty()Tests whether the stack is empty. -
push
Adds an element at the top of the stack.- Specified by:
push
in interfaceStack<E>
- Parameters:
e
- the element to be added- Throws:
IllegalStateException
- if the array storing the elements is full
-
top
Returns, but does not remove, the element at the top of the stack. -
pop
Removes and returns the top element from the stack. -
toString
Produces a string representation of the contents of the stack. (ordered from top to bottom). This exists for debugging purposes only. -
main
Demonstrates sample usage of a stack.
-