import jsjf.exceptions.*;
public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } public CircularArrayQueue() { this(DEFAULT_CAPACITY); } public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length *2]); for (int scan = 0; scan < count; scan++) { larger[scan] = queue[front]; front = (front + 1) % queue.length; } front = 0; rear = count; queue = larger; } public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = queue[front]; return result; // To be completed as a Programming Project } public boolean isEmpty() { return (count == 0); } public int size() { return count; } public String toString() { String queueElements = ""; for(int i = 0; i < rear; i++) { if(queue[i] != null) { queueElements = queueElements + queue[i].toString() + " "; } } return queueElements; } }
public class LinearNode { private LinearNode next; private T element; public LinearNode() { next = null; element = null; } public LinearNode(T elem) { next = null; element = elem; } public LinearNode getNext() { return next; } public void setNext(LinearNode node) { next = node; } public T getElement() { return element; } public void setElement(T elem) { element = elem; } }
How to create a DropoutCircularArrayQueue Class, which has a fixed size and drops out the oldest item in the queue if the queue becomes full.Create a DropoutCircularArrayQueue Class, which has a fixed size and drops out the oldest item in the queue if the queue becomes full.