public class SequenceQueue<T> { private int DEFAULT_SIZE = 10; // save the length of the array private int capacity; // Define an array to hold the elements of the sequential queue private Object[] elementData; // Save the current number of elements in the sequential queue private int front = 0; private int rear = 0; // Create empty sequential queue with default array length public SequenceQueue() { capacity = DEFAULT_SIZE; elementData = new Object[capacity]; } // create a sequential queue with an initializer element public SequenceQueue(T element) { this(); elementData[0] = element; rear++; } /** * Create a sequential queue with an array of specified length * @param element specifies the first element in the sequence queue * @param initSize specifies the length of the underlying array of the sequential queue */ public SequenceQueue(T element, int initSize) { this.capacity = initSize; elementData = new Object[capacity]; elementData[0] = element; rear++; } // Get the size of the sequential queue public int length() { return rear -front; } // Determine if the sequence queue is empty public boolean empty() { return rear == front; } // insert into the queue public void add(T element) { if (rear > capacity - 1) { throw new IndexOutOfBoundsException("Queue full exception"); } elementData[rear++] = element; } // remove queue public T remove() { if (empty()) { throw new IndexOutOfBoundsException("empty queue exception"); } // retain the value of the element on the rear side of the queue T oldValue = (T)elementData[front]; // Release the element on the rear side of the queue elementData[front++] = null; return oldValue; } // Return the top element of the queue, but do not delete the top element of the queue public T element() { if (empty()) { throw new IndexOutOfBoundsException("empty queue exception"); } return (T)elementData[front]; } // clear the sequence queue public void clear() { Arrays.fill(elementData, null); front = 0; rear = 0; } public String toString() { if (empty()) { return "[]"; } else { StringBuilder sb = new StringBuilder("["); for (int i = front; i < rear; i++) { sb.append(elementData[i].toString() + ", "); } int len = sb.length(); return sb.delete(len - 2, len).append("]").toString(); } } }
public class SequenceQueueTest { public static void main(String[] args) { SequenceQueue<String> queue = new SequenceQueue<String>(); queue.add("aaaa"); queue.add("bbbb"); queue.add("cccc"); queue.add("dddd"); System.out.println("Sequential queue initialization element: " + queue.toString()); System.out.println("Access the front-end element of the sequential queue: " + queue.element()); System.out.println("Remove the front element of the sequence queue: " + queue.remove()); System.out.println("After removing the front element for the first time: " + queue.toString()); System.out.println("Remove the front element of the sequence queue: " + queue.remove()); System.out.println("After removing the front element for the second time: " + queue.toString()); } }