Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class CircularArrayQueue implements MyQueue {
- private Integer[] array;
- private int noItems, tail, head;
- public CircularArrayQueue() {
- array = new Integer[10];
- noItems = 0;
- tail = 0;
- head = 0;
- }
- public int getCapacityLeft() {
- return array.length - noItems;
- }
- public void enqueue(int n) {
- if (noItems == array.length)
- resize();
- array[tail] = n;
- tail = (tail + 1) % array.length;
- noItems++;
- }
- public int dequeue() throws NoSuchElementException {
- if (isEmpty())
- throw new NoSuchElementException();
- Integer deq = array[head];
- array[head] = null;
- head = (head + 1) % array.length;
- noItems--;
- return deq;
- }
- public int noItems() {
- return this.noItems;
- }
- public boolean isEmpty(){
- return (noItems == 0);
- }
- public void resize() {
- Integer[] resizedArray = new Integer[array.length * 2];
- for (int i = 0; i < noItems; i++) {
- resizedArray[i] = array[(head + i) % array.length];
- }
- array = resizedArray;
- head = 0;
- tail = noItems;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement