LoganBlackisle

Opgave 1 - ArrayQueue

Jan 8th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package queueopgaver;
  2.  
  3. import java.util.NoSuchElementException;
  4.  
  5. /**
  6.  * An implementation of a queue as a array.
  7.  */
  8. public class ArrayQueue implements QueueI {
  9.     private Object[] elements;
  10.     private int currentSize;
  11.     private int head;
  12.     private int tail;
  13.  
  14.     /**
  15.      * Constructs an empty queue.
  16.      */
  17.     public ArrayQueue() {
  18.         final int INITIAL_SIZE = 10;
  19.         elements = new Object[INITIAL_SIZE];
  20.         currentSize = 0;
  21.         head = 0;
  22.         tail = 0;
  23.     }
  24.  
  25.     /**
  26.      * Checks whether this queue is empty.
  27.      *
  28.      * @return true if this queue is empty
  29.      */
  30.     @Override
  31.     public boolean isEmpty() {
  32.         return currentSize == 0;
  33.     }
  34.  
  35.     /**
  36.      * Adds an element to the tail of this queue.
  37.      *
  38.      * @param newElement the element to add
  39.      */
  40.     @Override
  41.     public void enqueue(Object newElement) {
  42.         growIfNecessary();
  43.         currentSize++;
  44.         elements[tail] = newElement;
  45.         tail = tail + 1;
  46.     }
  47.  
  48.     /**
  49.      * Removes an element from the head of this queue.
  50.      *
  51.      * @return the removed element
  52.      */
  53.     @Override
  54.     public Object dequeue() {
  55.         if (currentSize == 0) {
  56.             throw new NoSuchElementException();
  57.         }
  58.         Object removed = elements[head];
  59.         head = head + 1;
  60.         currentSize--;
  61.         return removed;
  62.     }
  63.  
  64.     /**
  65.      * Grows the element array if the current size equals the capacity.
  66.      */
  67.     public void growIfNecessary() {
  68.         if (currentSize == elements.length) {
  69.             Object[] newElements = new Object[2 * elements.length];
  70.             for (int i = 0; i < elements.length; i++) {
  71.                 newElements[i] = elements[head + i];
  72.             }
  73.             elements = newElements;
  74.             head = 0;
  75.             tail = currentSize;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * Returns the head of this queue. The queue is unchanged.
  81.      *
  82.      * @return the head element
  83.      */
  84.     @Override
  85.     public Object getFront() {
  86.         if (head < 0) {
  87.             throw new NoSuchElementException();
  88.         }
  89.         return elements[head];
  90.     }
  91.  
  92.     /**
  93.      * The number of elements on the queue.
  94.      *
  95.      * @return the number of elements in the queue
  96.      */
  97.     @Override
  98.     public int size() {
  99.         return currentSize;
  100.     }
  101. }
Add Comment
Please, Sign In to add comment