Package cpsc331.collections
Class ArrayStack<E>
- java.lang.Object
-
- cpsc331.collections.ArrayStack<E>
-
- All Implemented Interfaces:
BoundedStack<E>
public class ArrayStack<E> extends java.lang.Object implements BoundedStack<E>
Provides an implementation of a BoundedStack using a BoundedArray.
The constructor requires time linear in the capacity of the BoundedStack being created (because this time is required to initialize the BoundedArray being used). All other operations require constant time in the worst case.
-
-
Constructor Summary
Constructors Constructor Description ArrayStack(int maxSize)
Creates an initially empty bounded stack whose capacity is the input maxSize, if this is a positive int - throwing an IllegalArgumentException if maxSize is negative, instead.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
capacity()
Reports the capacity of this bounded stack.boolean
isEmpty()
Reports whether the stack is currently empty without changing it.E
peek()
Reports the top element of the stack without changing it.E
pop()
Removes an element from the top of the stack and reports it.void
push(E x)
Pushes a new entry onto the stack.
-
-
-
Constructor Detail
-
ArrayStack
public ArrayStack(int maxSize)
Creates an initially empty bounded stack whose capacity is the input maxSize, if this is a positive int - throwing an IllegalArgumentException if maxSize is negative, instead.- Parameters:
maxSize
- the capacity of the BoundedStack to be created
-
-
Method Detail
-
push
public void push(E x) throws StackFullException
Description copied from interface:BoundedStack
Pushes a new entry onto the stack.- Specified by:
push
in interfaceBoundedStack<E>
- Parameters:
x
- the object to be pushed onto the stack- Throws:
StackFullException
- if the Stack is already full
Precondition:
- The BoundedStack Invariant is satisfied.
- An element x of type E has been given as input.
- The BoundedStack Invariant is satisfied.
- If the number of objects presently on this bounded stack was less than the bounded stack's capacity, then the input object x has been pushed onto the top of the stack (which is otherwise unchanged).
- If the number of objects presently on the bounded stack was equal to the bounded stack's capacity, then a StackFullException is thrown and the stack is not changed.
-
peek
public E peek()
Description copied from interface:BoundedStack
Reports the top element of the stack without changing it.- Specified by:
peek
in interfaceBoundedStack<E>
- Returns:
- the element at the top of the stack
-
pop
public E pop()
Description copied from interface:BoundedStack
Removes an element from the top of the stack and reports it.- Specified by:
pop
in interfaceBoundedStack<E>
- Returns:
- the element removed from the stack
-
isEmpty
public boolean isEmpty()
Description copied from interface:BoundedStack
Reports whether the stack is currently empty without changing it.- Specified by:
isEmpty
in interfaceBoundedStack<E>
- Returns:
- true if the stack is empty, and false otherwise
Precondition:
- The BoundedStack Invariant is satisfied.
- The BoundedStack Invariant is satisfied.
- The bounded stack is not changed.
- If the bounded stack was empty before this operation then “true” is returned as output.
- If the bounded stack was not empty before this operation then “false” is returned as output.
-
capacity
public int capacity()
Description copied from interface:BoundedStack
Reports the capacity of this bounded stack.- Specified by:
capacity
in interfaceBoundedStack<E>
- Returns:
- the capacity of this BoundedStack
Precondition:
- The BoundedStack Invariant is satisfied.
- The BoundedStack Invariant is satisfied.
- The bounded stack is not changed.
- The capacity of this bounded stack is returned as output.
-
-