java——数组队列 ArrayQueue

队列:

  Array:

package Date_pacage;

public class Array<E> {
    //叫它静态数组
    //private int[] data;
    private E[] data;
    private int size;
    //构造函数
    public Array(int capacity) {
        data = (E[])new Object[capacity];
        size = 0;
    }
    //无参数的构造函数,默认数组的容量为10
    public Array() {
        this(10);
    }
    public int getSize() {
        return size;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public int getCapacity() {
        return data.length;
    }
    // O(1)
    public void addLast(E e) {
        add(size, e);
    }
    // O(n)
    public void addFirst(E e) {
        add(0, e);
    }
    // O(n/2) = O(n)
    public void add(int index, E e) {
        if(size>=data.length)
            resize(2 *data.length);
        if(index<0 || index>size)
            throw new IllegalArgumentException("Add failed.index is error.");
        for(int i=size-1;i>=index;i--) {
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
        res.append("[");
        for(int i = 0 ; i<size ; i++) {
            res.append(data[i]);
            if(i != size - 1)
                res.append(", ");
        }
        res.append("]");
        return res.toString();
    }
    public E get(int index) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        return data[index];
    }
    public E getFirst() {
        return get(size - 1);
    }
    public E getLast() {
        return get(0);
    }
    void set(int index, E e) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        data[index] = e;
    }
    public boolean contains(E e) {
        for(int i = 0; i < size; i++) {
            if(data[i].equals(e))
                return true;
        }
        return false;
    }
    public int find(E e) {
        for(int i = 0; i < size; i++) {
            if(data[i].equals(e))
                return i;
        }
        return -1;
    }
    public E remove(int index) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        E res = data[index];
        for(int i = index; i<size; i++) {
            data[i] = data[i+1];
        }
        size--;
        //释放空间,也可以不写 
        //loitering objects != memory leak
        data[size] = null;
        if(size == data.length / 4 && data.length / 2 != 0)
            resize(data.length / 2);
        return res;
    }
    public E removeFirst() {
        return remove(0);
    }
    public E removeLast() {
        return remove(size-1);
    }
    //只删除了一个e,并不能保证删除了全部e
    public void removeElement(E e) {
        int index = find(e);
        if(index != -1)
            remove(index);
    }
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for(int i=0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }
}

  队列接口:

public interface Queue<E> {
    int getSize();
    boolean isEmpty();
    void enqueue(E e);
    E dequeue();
    E getFront();
}

  数组队列:

package Date_pacage;

public class ArrayQueue<E> implements Queue<E> {
    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<>();
        for(int i = 0 ; i < 10 ; i++) {
            queue.enqueue(i);
            System.out.println(queue);
        }
    }
    private Array<E> array;
    public ArrayQueue(int capacity) {
        array = new Array<>(capacity);
    }
    public ArrayQueue() {
        array = new Array<>();
    }
    @Override
    public int getSize() {
        return array.getSize();
    }
    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }
    public int getCapacity() {
        return array.getCapacity();
    }
    @Override
    public void enqueue(E e) {
        array.addLast(e);
    }
    @Override
    public E dequeue() {
        return array.removeFirst();
    }
    @Override
    public E getFront() {
        return array.getFirst();
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue:");
        //队头
        res.append("front [");
        for(int i = 0 ; i < array.getSize() ; i++) {
            res.append(array.get(i));
            if(i != array.getSize() - 1)
                res.append(", ");
        }
        //队尾
        res.append("] tail");
        return res.toString();
    }
}

循环队列:  

  队列为空:front == tail 

  队列满:(tail+1)%data.length == front

package Date_pacage;

public class LoopQueue<E> implements Queue<E> {
    private E[] data;
    int front, tail;
    private int size;
    public LoopQueue(int capacity) {
        data = (E[])new Object[capacity+1];
        front = 0;
        tail = 0;
        size = 0;
    }
    public LoopQueue() {
        this(10);
    }
    public int getCapacity() {
        return data.length-1;
    }
    @Override
    public boolean isEmpty() {
        return front == tail;
    }
    @Override
    public int getSize() {
        return size;
    }
    @Override
    public void enqueue(E e) {
        if((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size ++;
    }
    private void resize(int newCapacity) {
        E[] newData = (E[])new Object[newCapacity + 1];
        for(int i = 0 ; i < size ; i++)
            newData[i] = data[(i + front) % data.length];
        data = newData;
        front = 0;
        tail = size;
    }
    @Override
    public E dequeue() {
        if(isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        }
        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size --;
        if(size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity()/2);
        }
        return ret;
    }
    @Override
    public E getFront() {
        if(isEmpty()) {
            throw new IllegalArgumentException("Queue is empty.");
        }
        return data[front];
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity()));
        res.append("front [");
        for(int i = 0 ; i != tail ; i = (i+1)%data.length) {
            res.append(data[i]);
            if((i+1)%data.length != tail)
                res.append(", ");
        }
        res.append("] tail");
        return res.toString();
    }
}

 

posted @ 2018-10-15 21:22  高圈圈  阅读(4038)  评论(0编辑  收藏  举报