Advertisement
Vita_Harvey

UnboundedArrayQueue_C

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