Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package queue;
- public class ArrayQueue extends AbstractQueue {
- private static final int DEFAULT_CAPACITANCE = 100;
- private static final int DEFAULT_CAPACITANCE_INCREASE_FACTOR = 2;
- private int capacitance;
- private Object[] data;
- private int head = 0;
- private int tail = 0;
- public ArrayQueue() {
- this.capacitance = DEFAULT_CAPACITANCE;
- data = new Object[capacitance];
- }
- public ArrayQueue(int capacitance) {
- this.capacitance = capacitance;
- data = new Object[capacitance];
- }
- @Override
- public void enqueue(Object object) {
- if (size() == capacitance - 1) {
- increaseCapacitance();
- }
- data[tail] = object;
- tail = (tail + 1) % capacitance;
- }
- @Override
- public Object element() {
- if (super.isEmpty()) {
- return null;
- }
- return data[head];
- }
- @Override
- public Object dequeue() {
- if (super.isEmpty()) {
- return null;
- }
- Object result = element();
- head = (head + 1) % capacitance;
- return result;
- }
- @Override
- public int size() {
- return head > tail ? capacitance - head + tail : tail - head;
- }
- @Override
- public void clear() {
- head = tail = 0;
- }
- @Override
- public Object[] toArray() {
- Object[] result = new Object[size()];
- int start = head;
- int i = 0;
- while (start != tail) {
- result[i] = data[start];
- ++i;
- start = (start + 1) % capacitance;
- }
- return result;
- }
- private void increaseCapacitance() {
- int newCapacitance = capacitance * ArrayQueue.DEFAULT_CAPACITANCE_INCREASE_FACTOR;
- int savedSize = size();
- Object[] newData = new Object[newCapacitance];
- int i = 0;
- while (head != tail) {
- newData[i] = data[head];
- ++i;
- head = (head + 1) % capacitance;
- }
- data = newData;
- head = 0;
- tail = savedSize;
- capacitance = newCapacitance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement