Advertisement
Vita_Harvey

UnboundedArrayQueue_D

Apr 3rd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. import java.lang.Throwable;
  4.  
  5.  
  6. /**
  7.  * @author Vita Wiebe
  8.  * @version PA5
  9.  */
  10. public class UnboundedArrayQueue<E extends Object> implements UnboundedQueue<E> {
  11.  
  12.    // Fields:
  13.    
  14.    // unbAQueue is our underlying array object.
  15.    // O(n)
  16.    Object[] unbAQueue;
  17.    
  18.    // Capacity is the number of elements that can be
  19.    // contained in UnbAQueue in its current incarnation.
  20.    private int capacity;
  21.  
  22.    /**
  23.     * O(n)
  24.     * A default constructor, creates underlying array
  25.     * with 10 elements.
  26.     * @param Int capacity, how many elements it should
  27.     * initially be able to contain.
  28.     */
  29.    public UnboundedArrayQueue(int capacity) throws QueueIsEmpty {
  30.       unbAQueue = new Object[capacity];
  31.    }
  32.    
  33.    public UnboundedArrayQueue() throws QueueIsEmpty {
  34.       unbAQueue = new Object[10];
  35.    }
  36.    
  37.     /**
  38.      * Add adds the object in question, o, to the queue
  39.      * in a FIFO manner.
  40.      * O(n^2)
  41.      * @param E e
  42.      */
  43.     public void add(E e) {
  44.       // Temp variable declared in case
  45.       // needed to adjust storage capacity.
  46.       Object[] tempQueue = new Object[size() * 2];
  47.      
  48.       if(hasSpace()) {
  49.          unbAQueue[size()] = (Object)e;
  50.       } else {
  51.          // New array with an additional storage.
  52.          tempQueue = new Object[unbAQueue.length * 2];
  53.          for(int i = 0; i < unbAQueue.length - 1; i++) {
  54.             tempQueue[i] = unbAQueue[i];
  55.          }
  56.          tempQueue[size()] = (Object)e;
  57.       }
  58.  
  59.       // Set the new array to the inital array while disposing
  60.       // of the inital array.
  61.       unbAQueue = tempQueue;
  62.     }
  63.    
  64.     /**
  65.      * Remove removes first object added to the queue.
  66.      * O(n^2)
  67.      * (Elements are removed in the order they are added, FIFO).
  68.      * @throws NullPointerException
  69.      */
  70.     public E remove() {
  71.    
  72.       // New array with one less element.
  73.       Object[] tempQueue = new Object[unbAQueue.length];
  74.      
  75.       // "removed" is the Object taken from the head
  76.       // of the queue.
  77.       Object removed = null;
  78.      
  79.       if(hasContents()) {
  80.          removed = (E)unbAQueue[0];
  81.          // Copy all the elements from the initial array
  82.          // to new array.
  83.          for(int i = 0; i < size(); i++){
  84.             tempQueue[i] = unbAQueue[i + 1];
  85.             }
  86.       }
  87.    
  88.       // Set the new array to the inital array while disposing
  89.       // of the inital array.
  90.       unbAQueue = tempQueue;        
  91.  
  92.       return (E)removed;
  93.     }
  94.    
  95.     /**
  96.      * O(k)
  97.      * @param None
  98.      * @return The first element added to the queue (
  99.      * i.e. the last element.
  100.      */
  101.     public E peek() {
  102.      
  103.       try {
  104.           if (unbAQueue.length == 0) {
  105.               throw new QueueIsEmpty("Empty.");
  106.           }
  107.           return (E)unbAQueue[unbAQueue.length - 1];
  108.       }catch(QueueIsEmpty q) {
  109.           System.out.println(q.getMessage());
  110.       }
  111.       return (E)unbAQueue[0];        
  112.     }
  113.    
  114.     /**
  115.      * O(n^2)
  116.      * Checks to see whether the queue has capacity to add more elements.
  117.      * @param None
  118.      * @return boolean
  119.      */
  120.     public boolean hasSpace() {
  121.    
  122.       for (int i=0; i < unbAQueue.length; i++){
  123.          if (unbAQueue[i] == null) {
  124.             return true;
  125.          }
  126.       }  
  127.       return false;
  128.     }
  129.    
  130.     /**
  131.      * O(n^2)
  132.      * Checks to see whether the queue has had any elements added to it.
  133.      * @param None
  134.      * @return boolean
  135.      */
  136.     public boolean hasContents() {
  137.    
  138.       for (int i = 0; i < unbAQueue.length; i++){
  139.          if(unbAQueue[i] != null) {
  140.             return true;
  141.          }
  142.       }
  143.       return false;
  144.     }
  145.    
  146.     /**
  147.      * O(n^2)
  148.      * @param None
  149.      * @return The number of elements that are currently stored in the structure.
  150.      */
  151.     public int size() {
  152.    
  153.       // Tracks the number of elements that aren't null (empty).
  154.       int count = 0;
  155.       if(hasContents()) {
  156.          for(int i = 0; i < unbAQueue.length - 1; i++) {
  157.             if((Integer)unbAQueue[i] != 0){
  158.                count++;
  159.             }
  160.          }
  161.       }
  162.       return count;
  163.     }
  164.    
  165.     /**
  166.      * O(n^2)
  167.      * @param None
  168.      * @return A string representation of the structure.
  169.      */
  170.     public String toString() {
  171.    
  172.       String stringVersion = new String("");
  173.      
  174.       for (int i = 0; i < unbAQueue.length - 1; i++){
  175.          stringVersion += (String)unbAQueue[i];
  176.       }
  177.       return stringVersion;              
  178.     }
  179.    
  180.     /**
  181.      * An overload of toString to return only
  182.      * a String representation of the first item in queue.
  183.      * @param Object current, the object at head of
  184.      * the queue.
  185.      */
  186.     public String toString(E current) {
  187.      
  188.       String stringVersion = new String("" + current + " ");
  189.       return stringVersion;
  190.     }
  191.    
  192.     public static void main(String[] args) throws QueueIsEmpty {
  193.    
  194.       UnboundedArrayQueue ourQueue1 = new UnboundedArrayQueue<String>();
  195.       ourQueue1.add("Miser");
  196.       ourQueue1.add("Dinge");
  197.       ourQueue1.add("Schoopat");
  198.       ourQueue1.add("Biffwaff");
  199.       System.out.println(ourQueue1.toString() + " ");
  200.       System.out.println(ourQueue1.toString(ourQueue1.peek()));
  201.     }
  202.    
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement