Vita_Harvey

UnboundedArrayQueue_A

Feb 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. /**
  4.  * @author Vita Wiebe
  5.  * @version PA5
  6.  */
  7. public class UnboundedArrayQueue implements UnboundedQueue {
  8.  
  9.    // Fields:
  10.    
  11.    // unbAQueue is our underlying array object.
  12.    // O(n)
  13.    Object[] unbAQueue;
  14.    
  15.    // Capacity is the number of elements that can be
  16.    // contained in UnbAQueue in its current incarnation.
  17.    private int capacity;
  18.  
  19.    /**
  20.     * O(n)
  21.     * A default constructor, creates underlying array
  22.     * with 10 elements.
  23.     * @param None
  24.     */
  25.    public UnboundedArrayQueue() {
  26.       unbAQueue = new Object[10];
  27.    }
  28.    
  29.    /**
  30.     * O(n)
  31.     * Capacity-specific constructor, creates underlying array
  32.     * with "capacity" elements.
  33.     * @param None
  34.     */
  35.    public UnboundedArrayQueue(int capacity) {
  36.       unbAQueue = new Object[capacity];
  37.    }
  38.      
  39.    /**
  40.      * Add adds the object in question, o, to the queue
  41.      * in a FIFO manner.
  42.      * O(n^2)
  43.      * @param Object o
  44.      */
  45.     public void add(Object o) {
  46.      
  47.       Object[] tempQueue;
  48.      
  49.       if(hasSpace()) {
  50.          // New temp array.
  51.          tempQueue = new Object[unbAQueue.length];
  52.                  
  53.       }else {
  54.          // New temp array with twice the number of elements.
  55.          tempQueue = new Object[unbAQueue.length * 2];
  56.       }
  57.       // Copy all the elements from the initial array
  58.       // to new temp array, and at 1 index value ahead of original
  59.       // index.
  60.       for(int i = 0; i < unbAQueue.length - 1; i++){
  61.          tempQueue[i + 1] = unbAQueue[i];
  62.       }
  63.      
  64.       // Assign the new element to the last index position,
  65.       // which was the first element added.
  66.       tempQueue[0] = o;
  67.  
  68.       // Set the new array to the inital array while disposing
  69.       // of the inital array.
  70.       unbAQueue = tempQueue;          
  71.     }
  72.    
  73.     /**
  74.      * Remove removes first object added to the queue.
  75.      * O(n^2)
  76.      * (Elements are removed in the order they are added, FIFO).
  77.      * @throws NullPointerException
  78.      */
  79.     public Object remove() {
  80.    
  81.       // New array with one less element.
  82.       Object[] tempQueue = new Object[unbAQueue.length - 1];
  83.      
  84.       // Copy all the elements from the initial array
  85.       // to new array.
  86.       for(int i = 0; i < unbAQueue.length - 2; i++){
  87.          if((unbAQueue[i]) == "0") {
  88.             tempQueue[i] = 0;
  89.          }else if(unbAQueue[i] == null) {
  90.             throw new NullPointerException();
  91.          }else{
  92.             tempQueue[i] = unbAQueue[i];
  93.          }
  94.       // Remove the element first added to the queue,
  95.       // i.e. the last element in array.
  96.       tempQueue[tempQueue.length - 1] = null;
  97.  
  98.       // Set the new array to the inital array while disposing
  99.       // of the inital array.
  100.       unbAQueue = tempQueue;        
  101.       }
  102.       return unbAQueue;
  103.     }
  104.    
  105.     /**
  106.      * O(k)
  107.      * @param None
  108.      * @return The first element added to the queue (
  109.      * i.e. the last element; null if empty.
  110.      */
  111.     public Object peek() {
  112.    
  113.       if (unbAQueue.length == 0) {
  114.          return null;
  115.       }else{
  116.          return unbAQueue[unbAQueue.length - 1];
  117.       }
  118.     }
  119.    
  120.     /**
  121.      * O(n^2)
  122.      * Checks to see whether the queue has capacity to add more elements.
  123.      * @param None
  124.      * @return boolean
  125.      */
  126.     public boolean hasSpace() {
  127.    
  128.       for (int i=0; i < unbAQueue.length; i++){
  129.          if (unbAQueue[i] == null) {
  130.             return true;
  131.          }
  132.       }  
  133.       return false;
  134.     }
  135.    
  136.     /**
  137.      * O(n^2)
  138.      * Checks to see whether the queue has had any elements added to it.
  139.      * @param None
  140.      * @return boolean
  141.      */
  142.     public boolean hasContents() {
  143.    
  144.       for (int i = 0; i < unbAQueue.length; i++){
  145.          if(unbAQueue[i] != null) {
  146.             return true;
  147.          }
  148.       }
  149.       return false;
  150.     }
  151.    
  152.     /**
  153.      * O(n^2)
  154.      * @param None
  155.      * @return The number of elements that are currently stored in the structure.
  156.      */
  157.     public int size() {
  158.    
  159.       // Tracks the number of elements that aren't null (empty).
  160.       int count = 0;
  161.      
  162.       for(int i = 0; i < unbAQueue.length; i++) {
  163.          if(unbAQueue[i] != null){
  164.             count++;
  165.          }
  166.       }
  167.       return count;
  168.     }
  169.  
  170. }
Add Comment
Please, Sign In to add comment