Advertisement
Vita_Harvey

UnboundedArrayStack_A

Mar 10th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. /**
  4.  * @author Vita Wiebe
  5.  * @version PA5
  6.  */
  7. public class UnboundedArrayStack<E> implements UnboundedStack {
  8.  
  9.    // Fields:
  10.    
  11.    // UnbAStack is our underlying array object.
  12.    // O(n)
  13.    Object[] unbAStack;
  14.    
  15.    // Capacity is the number of elements that can be
  16.    // contained in bStack.
  17.    // zero time op
  18.    private int capacity;
  19.    
  20.    /**
  21.     * O(n)
  22.     * A default constructor, creates underlying array
  23.     * with 10 elements.
  24.     * @param None
  25.     */
  26.    public UnboundedArrayStack() {
  27.       unbAStack = new Object[10];
  28.    }
  29.    
  30.    /**
  31.     * O(n)
  32.     * A default constructor, creates underlying array
  33.     * with capacity elements.
  34.     * @param None
  35.     */
  36.    public UnboundedArrayStack(int capacity) {
  37.       unbAStack = new Object[capacity];
  38.    }
  39.    
  40.    /**
  41.     * Push adds the object in question, o, to the top of the stack.
  42.     * O(n^2)
  43.     * @param Object o
  44.     */    
  45.    public void push(Object o){
  46.      
  47.       //Temporary variable to hold new array with shifted elements.
  48.       Object[] newStack;
  49.      
  50.       while(capacity > 2) {
  51.      
  52.          if(hasSpace()) {                
  53.             newStack = new Object[capacity];
  54.             // Place new object at the top of the stack.
  55.             newStack[0] = o;
  56.    
  57.             // Populates new array with elements of UnbAStack,
  58.             // beginning with second element (index 1) of newStack.      
  59.             for (int i = 1; i < unbAStack.length; i++) {
  60.                newStack[i] = unbAStack[i - 1];
  61.             }
  62.          }else{      
  63.             // Creates a new array with twice as many elements.
  64.             newStack = new Object [capacity * 2];
  65.            
  66.             // Place new object at the top of the stack.
  67.             newStack[0] = o;
  68.            
  69.             // Populates new array with elements of UnbAStack,
  70.             // beginning with second element (index 1) of newStack.
  71.             for (int i = 1; i < unbAStack.length; i++) {
  72.                newStack[i] = unbAStack[i - 1];
  73.             }                            
  74.          }
  75.          unbAStack = newStack;
  76.       }    
  77.     }
  78.    
  79.     /**
  80.      * Pop removes the last object added to the stack.
  81.      * O(n^2)
  82.      * @param None
  83.      * @return unbAStack, with last element added removed.
  84.      * @throws NullPointerException
  85.      */
  86.     public Object pop(){
  87.    
  88.       try {
  89.          // Remove element on the "top" of the stack (i.e. LIFO).
  90.          unbAStack[0] = null;
  91.          // Shift the elements down after the "pop".
  92.          for(int i = 0; i < unbAStack.length - 1; i++){
  93.             unbAStack[i] = unbAStack[i + 1];
  94.          }      
  95.       }catch(NullPointerException e){
  96.          throw new NullPointerException("Stack contains no elements.");  
  97.       }
  98.       return unbAStack;
  99.     }
  100.    
  101.     /**
  102.      * Peek lets the user see what the most-recently-added element in the stack is.
  103.      * O(n)
  104.      * @param None
  105.      * @throws NullPointerException
  106.      * @return Last object added to the stack.
  107.      */
  108.     public Object peek() {
  109.    
  110.       if (unbAStack.length == 0) {
  111.          return null;
  112.       }else{
  113.          return unbAStack[0];
  114.       }
  115.     }
  116.    
  117.     /**
  118.      * Checks to see whether the current version
  119.      * of the stack has capacity to add more elements.
  120.      * O(n^2)
  121.      * @param None
  122.      * @return boolean
  123.      */
  124.     public boolean hasSpace() {
  125.    
  126.       for (int i=0; i < unbAStack.length; i++){
  127.          if (unbAStack[i] == null) {
  128.             return true;
  129.          }
  130.       }  
  131.       return false;
  132.     }
  133.    
  134.     /**
  135.      * Checks to see whether the stack has had any elements added to it.
  136.      * O(n^2)
  137.      * @param None
  138.      * @return boolean
  139.      */
  140.     public boolean hasContents() {
  141.    
  142.       for (int i = 0; i < unbAStack.length; i++){
  143.          if(unbAStack[i] != null) {
  144.             return true;
  145.          }
  146.       }
  147.       return false;
  148.     }
  149.    
  150.     /**
  151.      * O(k)
  152.      * @param None
  153.      * @return The number of elements that are currently stored in the structure
  154.      */
  155.     public int size() {
  156.    
  157.       // Tracks the number of elements that aren't null (empty).
  158.       int count = 0;
  159.      
  160.       for(int i = 0; i < unbAStack.length; i++) {
  161.          if(unbAStack[i] != null){
  162.             count++;
  163.          }
  164.       }
  165.       return count;
  166.    }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement