Botre Posted May 25, 2015 Share Posted May 25, 2015 (edited) Cookie pls package org.bjornkrols.botre.datastructures.queue; import java.util.Iterator; import java.util.NoSuchElementException; /** * Created by Bjorn on 25/05/2015. * Last edit: 25/05/2015. */ public class CircularArrayQueue<T> implements Queue<T>, Iterable<T> { private T[] queue; private int front, insert, size; public CircularArrayQueue(int capacity){ queue = (T[]) new Object[capacity]; } /** * Inserts the element at the rear of the queue. * If the queue is full, the oldest value will be removed and the the second oldest element will become the front element. */ @Override public void enqueue(T element) { queue[insert] = element; insert = (insert + 1) % queue.length; if (isFull()) { front = (front + 1) % queue.length; } else { size++; } } @Override public T dequeue() { if (size == 0) { throw new NoSuchElementException("Queue is empty."); } T element = queue[front]; queue[front] = null; front = (front + 1) % queue.length; size--; return element; } @Override public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == queue.length; } @Override public int size() { return size; } @Override public Iterator<T> iterator() { return new QueueIterator(); } public class QueueIterator implements Iterator<T> { private int current = front; private int i; @Override public boolean hasNext() { return i < queue.length; } @Override public T next() { T value = queue[current]; current = (current + 1) % queue.length; i++; return value; } } } Edited May 25, 2015 by Botre Quote Link to comment Share on other sites More sharing options...
Orange Posted May 25, 2015 Share Posted May 25, 2015 such wow Quote Link to comment Share on other sites More sharing options...
Jack Posted May 25, 2015 Share Posted May 25, 2015 Replies are overrated. 1 Quote Link to comment Share on other sites More sharing options...
Volta Posted May 25, 2015 Share Posted May 25, 2015 How come OSBot's mouse does that sometimes 2 Quote Link to comment Share on other sites More sharing options...
Czar Posted May 25, 2015 Share Posted May 25, 2015 (edited) reminds me of the osbot mouse trail debug nevertheless, good job edit: nvm ^ my post was too l8 Edited May 25, 2015 by Czar 2 Quote Link to comment Share on other sites More sharing options...
Fantabulous Posted May 25, 2015 Share Posted May 25, 2015 1 Quote Link to comment Share on other sites More sharing options...
Botre Posted May 25, 2015 Author Share Posted May 25, 2015 nom nom nom Quote Link to comment Share on other sites More sharing options...