ArrayQueue(数组队列)

Queue定义,直接copyJava的Queue,去除了Collection接口。

public interface Queue<E> {
    
    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * {@code true} upon success and throwing an {@code IllegalStateException}
     * if no space is currently available.
     *
     * @param e the element to add
     * @return {@code true} (as specified by
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(E e);

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     *
     * @param e the element to add
     * @return {@code true} if the element was added to this queue, else
     *         {@code false}
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(E e);

    /**
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E remove();

    /**
     * Retrieves and removes the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E poll();

    /**
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception
     * if this queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E element();

    /**
     * Retrieves, but does not remove, the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E peek();
}

ArrayQueue

public class ArrayQueue implements Queue {

    // 数组
    int[] array;
    //最大值
    int maxSize;
    //
    int front;
    //
    int rear;

    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    public ArrayQueue(int maxSize) {
        this.front = -1;
        this.rear = -1;
        this.array = new int[maxSize];
        this.maxSize = maxSize;
    }

    @Override
    public boolean add(Object value) {
        reset();
        if (isFull()) {
            throw new IllegalArgumentException("queue if full, can not add!");
        }
        addDirectly(value);
        return  true;
    }

    @Override
    public boolean offer(Object value) {
        reset();
        if (isFull()) {
            System.out.println("queue if full, can not offer!");
            return false;
        }
        addDirectly(value);
        return  true;
    }

    @Override
    public Object remove() {
        if (isEmpty()) {
            throw new NoSuchElementException("queue if empty!");
        }
        return getDirectly();
    }

    @Override
    public Object poll() {
        if (isEmpty()) {
            return null;
        }
        return getDirectly();
    }

    @Override
    public Object element() {
        if (isEmpty()) {
            throw new NoSuchElementException("queue if empty!");
        }
        return array[front + 1];
    }

    @Override
    public Object peek() {
        if (isEmpty()) {
            return null;
        }
        return array[front + 1];
    }

    private boolean isEmpty() {
        return rear == front;
    }

    private void addDirectly(Object value) {
        this.array[++rear] = (int) value;
    }

    private int getDirectly() {
        ++front;
        int value = array[front];
        array[front] = 0;
        return value;
    }

    private boolean isFull() {
        if (this.rear == maxSize -1) {
            return true;
        }
        return false;
    }

    /**
     * 重置队列,希望可以有多余的空间
     */
    private void reset() {
        if (rear + 1 > maxSize * DEFAULT_LOAD_FACTOR) {
            if (front != -1) {
                int index = -1;
                int[] temp = new int[maxSize];
                for (int i = front + 1; i <= rear; i++) {
                    temp[++index] = this.array[i];
                }
                this.array = temp;
                this.rear = this.rear - front - 1;
                this.front = -1;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        ArrayQueue queue =  new ArrayQueue(2);
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("o(offer)");
            System.out.println("p(poll)");

            char cmd = scanner.next().charAt(0);
            switch (cmd) {
                case 'o' :
                     System.out.println("请输入值需要添加的值....");
                     int value = scanner.nextInt();
                     queue.offer(value);
                     System.out.println(String.format("queue is : %s", Arrays.toString(queue.array)));
                     break;
                case 'p' :
                    System.out.println(String.format("value is : %s", queue.poll()));
                    System.out.println(String.format("queue is : %s", Arrays.toString(queue.array)));
                    break;
                default:
                     System.out.println("未知的命令" + cmd);
                     break;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/sleepingDogs/p/11140640.html