SHARE
TWEET

ARRAYQUEUE

shchuko Feb 21st, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package queue;
  2.  
  3. public class ArrayQueue extends AbstractQueue {
  4.     private static final int DEFAULT_CAPACITANCE = 100;
  5.     private static final int DEFAULT_CAPACITANCE_INCREASE_FACTOR = 2;
  6.  
  7.     private int capacitance;
  8.     private Object[] data;
  9.     private int head = 0;
  10.     private int tail = 0;
  11.  
  12.     public ArrayQueue() {
  13.         this.capacitance = DEFAULT_CAPACITANCE;
  14.         data = new Object[capacitance];
  15.     }
  16.  
  17.     public ArrayQueue(int capacitance) {
  18.         this.capacitance = capacitance;
  19.         data = new Object[capacitance];
  20.     }
  21.  
  22.     @Override
  23.     public void enqueue(Object object) {
  24.         if (size() == capacitance - 1) {
  25.             increaseCapacitance();
  26.         }
  27.  
  28.         data[tail] = object;
  29.         tail = (tail + 1) % capacitance;
  30.     }
  31.  
  32.     @Override
  33.     public Object element() {
  34.         if (super.isEmpty()) {
  35.             return null;
  36.         }
  37.  
  38.         return data[head];
  39.     }
  40.  
  41.     @Override
  42.     public Object dequeue() {
  43.         if (super.isEmpty()) {
  44.             return null;
  45.         }
  46.  
  47.         Object result = element();
  48.         head = (head + 1) % capacitance;
  49.         return result;
  50.     }
  51.  
  52.     @Override
  53.     public int size() {
  54.         return head > tail ? capacitance - head + tail : tail - head;
  55.     }
  56.  
  57.     @Override
  58.     public void clear() {
  59.         head = tail = 0;
  60.     }
  61.  
  62.     @Override
  63.     public Object[] toArray() {
  64.         Object[] result = new Object[size()];
  65.  
  66.         int start = head;
  67.         int i = 0;
  68.         while (start != tail) {
  69.             result[i] = data[start];
  70.             ++i;
  71.             start = (start + 1) % capacitance;
  72.         }
  73.         return result;
  74.     }
  75.  
  76.     private void increaseCapacitance() {
  77.         int newCapacitance = capacitance * ArrayQueue.DEFAULT_CAPACITANCE_INCREASE_FACTOR;
  78.         int savedSize = size();
  79.  
  80.         Object[] newData = new Object[newCapacitance];
  81.  
  82.         int i = 0;
  83.         while (head != tail) {
  84.             newData[i] = data[head];
  85.             ++i;
  86.             head = (head + 1) % capacitance;
  87.         }
  88.  
  89.         data = newData;
  90.         head = 0;
  91.         tail = savedSize;
  92.         capacitance = newCapacitance;
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top