Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayQueueModule {
- private static int head;
- private static int tail;
- private static Object[] queue = new Object[5];
- public static int size() {
- if (head > tail) {
- return queue.length - head + tail;
- } else {
- return tail - head;
- }
- }
- public static void enqueue(Object element) {
- assert element != null;
- ensureCapacity(size() + 1);
- queue[tail] = element;
- tail = (tail + 1) % queue.length;
- }
- private static void ensureCapacity(int capacity) {
- if (capacity <= queue.length) {
- return;
- }
- Object[] newQueue = new Object[2 * capacity];
- for (int i = 0; i < size(); i++) {
- newQueue[i] = queue[i];
- }
- queue = newQueue;
- }
- public static Object element() {
- assert size() > 0;
- return queue[head];
- }
- public static Object dequeue() {
- assert size() > 0;
- Object x = queue[head];
- head = (head + 1) % queue.length;
- return x;
- }
- public static boolean isEmpty() {
- return head == tail;
- }
- public static void clear() {
- head = 0;
- tail = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement