Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.data_structures;
- /**
- * @author Vita Wiebe
- * @version PA5
- */
- public class UnboundedArrayStack<E> implements UnboundedStack {
- // Fields:
- // UnbAStack is our underlying array object.
- // O(n)
- Object[] unbAStack;
- // Capacity is the number of elements that can be
- // contained in bStack.
- // zero time op
- private int capacity;
- /**
- * O(n)
- * A default constructor, creates underlying array
- * with 10 elements.
- * @param None
- */
- public UnboundedArrayStack() {
- unbAStack = new Object[10];
- }
- /**
- * O(n)
- * A default constructor, creates underlying array
- * with capacity elements.
- * @param None
- */
- public UnboundedArrayStack(int capacity) {
- unbAStack = new Object[capacity];
- }
- /**
- * Push adds the object in question, o, to the top of the stack.
- * O(n^2)
- * @param Object o
- */
- public void push(Object o){
- //Temporary variable to hold new array with shifted elements.
- Object[] newStack;
- while(capacity > 2) {
- if(hasSpace()) {
- newStack = new Object[capacity];
- // Place new object at the top of the stack.
- newStack[0] = o;
- // Populates new array with elements of UnbAStack,
- // beginning with second element (index 1) of newStack.
- for (int i = 1; i < unbAStack.length; i++) {
- newStack[i] = unbAStack[i - 1];
- }
- }else{
- // Creates a new array with twice as many elements.
- newStack = new Object [capacity * 2];
- // Place new object at the top of the stack.
- newStack[0] = o;
- // Populates new array with elements of UnbAStack,
- // beginning with second element (index 1) of newStack.
- for (int i = 1; i < unbAStack.length; i++) {
- newStack[i] = unbAStack[i - 1];
- }
- }
- unbAStack = newStack;
- }
- }
- /**
- * Pop removes the last object added to the stack.
- * O(n^2)
- * @param None
- * @return unbAStack, with last element added removed.
- * @throws NullPointerException
- */
- public Object pop(){
- try {
- // Remove element on the "top" of the stack (i.e. LIFO).
- unbAStack[0] = null;
- // Shift the elements down after the "pop".
- for(int i = 0; i < unbAStack.length - 1; i++){
- unbAStack[i] = unbAStack[i + 1];
- }
- }catch(NullPointerException e){
- throw new NullPointerException("Stack contains no elements.");
- }
- return unbAStack;
- }
- /**
- * Peek lets the user see what the most-recently-added element in the stack is.
- * O(n)
- * @param None
- * @throws NullPointerException
- * @return Last object added to the stack.
- */
- public Object peek() {
- if (unbAStack.length == 0) {
- return null;
- }else{
- return unbAStack[0];
- }
- }
- /**
- * Checks to see whether the current version
- * of the stack has capacity to add more elements.
- * O(n^2)
- * @param None
- * @return boolean
- */
- public boolean hasSpace() {
- for (int i=0; i < unbAStack.length; i++){
- if (unbAStack[i] == null) {
- return true;
- }
- }
- return false;
- }
- /**
- * Checks to see whether the stack has had any elements added to it.
- * O(n^2)
- * @param None
- * @return boolean
- */
- public boolean hasContents() {
- for (int i = 0; i < unbAStack.length; i++){
- if(unbAStack[i] != null) {
- return true;
- }
- }
- return false;
- }
- /**
- * O(k)
- * @param None
- * @return The number of elements that are currently stored in the structure
- */
- public int size() {
- // Tracks the number of elements that aren't null (empty).
- int count = 0;
- for(int i = 0; i < unbAStack.length; i++) {
- if(unbAStack[i] != null){
- count++;
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement