【數據結構】隊列之循環隊列

public class LoopQueue<T> {

    private int DEFAULT_SIZE = 10;
    // 保存數組的長度
    private int capacity;
    // 定義一個數組用於保存循環隊列的元素
    private Object[] elementData;
    // 保存循環隊列中元素的當前個數
    private int front = 0;
    private int rear = 0;

    // 以默認數組長度建立空循環隊列
    public LoopQueue() {
        capacity = DEFAULT_SIZE;
        elementData = new Object[capacity];
    }

    // 以一個初始化元素來建立循環隊列
    public LoopQueue(T element) {
        this();
        elementData[0] = element;
        rear++;
    }

    /**
     * 以指定長度的數組來建立循環隊列
     *
     * @param element  指定循環隊列中第一個元素
     * @param initSize 指定循環隊列底層數組的長度
     */
    public LoopQueue(T element, int initSize) {
        this.capacity = initSize;
        elementData = new Object[capacity];
        elementData[0] = element;
        rear++;
    }

    // 判斷循環隊列是否爲空隊列
    public boolean empty() {
        // rear == front且rear處元素爲null
        return rear == front && elementData[rear] == null;
    }

    //獲取循環隊列的大小
    public int length() {
        if (empty()) {
            return 0;
        } else {
            return rear > front ? rear - front : capacity - (front - rear);
        }
    }

    // 插入隊列
    public void add(T element) {
        if (rear == front && elementData[front] != null) {
            throw new IndexOutOfBoundsException("隊列已滿的異常");
        }
        elementData[rear++] = element;
        // 若是rear已經到頭,那就轉頭
        rear = rear == capacity ? 0 : rear;
    }

    // 移除隊列
    public T remove() {
        if (empty()) {
            throw new IndexOutOfBoundsException("空隊列異常");
        }
        // 保留隊列的rear端的元素的值
        T oldValue = (T) elementData[front];
        // 釋放隊列的rear端的元素
        elementData[front++] = null;
        // 若是front已經到頭,那就轉頭
        front = front == capacity ? 0 : front;
        return oldValue;
    }

    // 返回隊列頂元素,但不刪除隊列頂元素
    public T element() {
        if (empty()) {
            throw new IndexOutOfBoundsException("空隊列異常");
        }
        return (T) elementData[front];
    }

    // 清空循環隊列
    public void clear() {
        // 將底層數組全部元素賦爲null
        Arrays.fill(elementData, null);
        front = 0;
        rear = 0;
    }

    public String toString() {
        if (empty()) {
            return "[]";
        } else {
            // 若是front < rear,有效元素就是就是front到rear之間的元素
            if (front < rear) {
                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();
            }
            // 若是front >= rear,有效元素爲front -> capacity之間、0 -> front之間的
            else {
                StringBuilder sb = new StringBuilder("[");
                for (int i = front; i < capacity; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                for (int i = 0; i < rear; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                int len = sb.length();
                return sb.delete(len - 2, len).append("]").toString();
            }
        }
    }
}
public class LoopQueueTest {
    public static void main(String[] args) {
        LoopQueue<String> queue = new LoopQueue<String>("aaaa", 3);
        // 新增元素
        queue.add("bbbb");
        queue.add("cccc");
        System.out.println("初始化元素列表:" + queue.toString());
        queue.remove();
        System.out.println("刪除最後一個元素列表:" + queue);
        queue.add("dddd");
        System.out.println("再次添加一個元素列表:" + queue);
        System.out.println("隊列滿時的長度:" + queue.length());
        queue.remove();
        queue.add("eeee");
        System.out.println("循環隊列中元素:" + queue);
    }
}
相關文章
相關標籤/搜索