Package cpsc331.collections
Interface BoundedStack<E>
-
- All Known Implementing Classes:
ArrayStack
public interface BoundedStack<E>
Provides an interface for a bounded stack whose elements are of type E.
BoundedStack Invariant: A collection of objects of type E is maintained in last-in first-out order: The object that is visible at the top of the stack is the object that has most recently been pushed onto it, and not yet removed. The number of objects on the stack is never allowed to exceed this stack's (positive integer) capacity.
-
-
Method Summary
All Methods Instance Methods Abstract 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.
-
-
-
Method Detail
-
push
void push(E x) throws StackFullException
Pushes a new entry onto the stack.- 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
E peek()
Reports the top element of the stack without changing it.- Returns:
- the element at the top of the stack
- Throws:
java.util.NoSuchElementException
- if the stack is empty
Precondition:
- The BoundedStack Invariant is satisfied.
- The BoundedStack invariant is satisfied.
- The bounded stack is not changed.
- If the bounded stack was not empty before this operation then the top element is returned as output.
- If the bounded stack was empty before this operation then a NoSuchElementException is thrown.
-
pop
E pop()
Removes an element from the top of the stack and reports it.- Returns:
- the element removed from the stack
- Throws:
java.util.NoSuchElementException
- if the stack is already empty
Precondition:
- The BoundedStack Invariant is satisfied.
- The BoundedStack Invariant is satisfied.
- If the bounded stack was not empty before this operation then the top element is removed from the stack (which is otherwise unchanged) and this element is returned as output.
- If the bounded stack was empty before this operation then a NoSuchElementException is thrown and the stack is not changed.
-
isEmpty
boolean isEmpty()
Reports whether the stack is currently empty without changing it.- 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
int capacity()
Reports the capacity of this bounded stack.- 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.
-
-