Advertisement
Vita_Harvey

UnboundedArrayQueue_B

Feb 20th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 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] == null) {
  88.             throw new NullPointerException();
  89.          }else{
  90.             tempQueue[i] = unbAQueue[i];
  91.          }
  92.       // Remove the element first added to the queue,
  93.       // i.e. the last element in array.
  94.       tempQueue[tempQueue.length - 1] = null;
  95.  
  96.       // Set the new array to the inital array while disposing
  97.       // of the inital array.
  98.       unbAQueue = tempQueue;        
  99.       }
  100.       return unbAQueue;
  101.     }
  102.    
  103.     /**
  104.      * O(k)
  105.      * @param None
  106.      * @return The first element added to the queue (
  107.      * i.e. the last element; null if empty.
  108.      */
  109.     public Object peek() {
  110.    
  111.       if (unbAQueue.length == 0) {
  112.          return null;
  113.       }else{
  114.          return unbAQueue[unbAQueue.length - 1];
  115.       }
  116.     }
  117.    
  118.     /**
  119.      * O(n^2)
  120.      * Checks to see whether the queue has capacity to add more elements.
  121.      * @param None
  122.      * @return boolean
  123.      */
  124.     public boolean hasSpace() {
  125.    
  126.       for (int i=0; i < unbAQueue.length; i++){
  127.          if (unbAQueue[i] == null) {
  128.             return true;
  129.          }
  130.       }  
  131.       return false;
  132.     }
  133.    
  134.     /**
  135.      * O(n^2)
  136.      * Checks to see whether the queue has had any elements added to it.
  137.      * @param None
  138.      * @return boolean
  139.      */
  140.     public boolean hasContents() {
  141.    
  142.       for (int i = 0; i < unbAQueue.length; i++){
  143.          if(unbAQueue[i] != null) {
  144.             return true;
  145.          }
  146.       }
  147.       return false;
  148.     }
  149.    
  150.     /**
  151.      * O(n^2)
  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 < unbAQueue.length; i++) {
  161.          if(unbAQueue[i] != null){
  162.             count++;
  163.          }
  164.       }
  165.       return count;
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement