《Java數據結構》Java Queue隊列

前言

學習LinkedList的時候,遇到了隊列數據結構,就想着回顧一下隊列數據結構。

原理

Queue數據結構是一種線性的結構,先進先出的特性。下面看一下邏輯圖

隊列在我們寫代碼的過程比較常用,例如打印日誌,一個程序打印日誌是比較消耗資源的,一般都會採用隊列實現,將需要的日誌先放入隊列中,由其他線程去

循環隊列輸出打印。

代碼

通過數組實現的定長隊列

public class QueueArray<E> {

    // 定義一個數組
    private Object[] data = null;

    // 記錄隊列長度
    private int size = 0;

    private int length = 0;

    private int point = 0;

    /**
     * 默認構造函數
     */
    public QueueArray(){
        length = 10;
        data = new Object[10];
    }

    /**
     * 自定義隊列長度
     * @param length
     */
    public QueueArray(int length){
        this.length = length;
        data = new Object[length];
    }

    /**
     * 往隊列裏面加入數據
     * @param e
     */
    public void push(E e){
        data[size++] = e;
        size = size%length;
    }

    /**
     * 往隊列裏面獲取數據
     */
    public E pull(){
        E e = (E)data[point];
        data[point] = null;
        if(point/length == 1){
            point = 0;
        } else {
            point++;
        }
        return e;
    }

    /**
     * 打印隊列
     */
    public void println(){
        for(int i =point ;i < size; i++){
            System.out.println(data[i]);
        }
    }
}
public class QueueTest {

    public static void main(String[] args) {
        QueueArray queueArray = new QueueArray();
        queueArray.push("1");
        queueArray.push(2);
        queueArray.push("5");
        System.out.println(queueArray.pull());
        queueArray.push(7);
        queueArray.push("9");
        System.out.println(queueArray.pull());
        System.out.println(queueArray.pull());
        queueArray.println();
    }
}

運行結果:

通過鏈表實現的可變長度隊列

public class QueueLinked<E> {

    /**
     * 創建內部類鏈表的節點
     * @param <E>
     */
    private class Node<E>{
        public E data = null; //數據域
        public Node<E> next = null; //指針域

        //構造方法
        public Node(){}

        public Node(E data){
            this.data = data;
        }
    }

    private Node<E> head = null; //隊頭
    private Node<E> end  = null; //隊尾
    private int size = 0;         //隊列長度

    /**
     * 判斷隊列是否爲空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 往隊列裏面加入數據
     * @param e
     */
    public boolean push(E e){
        Node<E> node = new Node<E>(e);
        //隊列爲空的情況下
        if(isEmpty()){
            this.end = node; //尾節點賦值爲新插入的節點
            this.head = this.end; //在只有一個節點的情況下,頭尾節點相等
            size++;
            return true;
        }
        //不爲空的情況下
        this.end.next = node; //尾節點指向新節點
        this.end = node; //更新尾節點
        size ++; //隊列節點數+1
        return true;
    }

    /**
     * 出隊
     * @return
     */
    public E pop(){
        //判斷隊列是否爲空
        if(isEmpty()){
            System.err.println("隊列爲空");
            return null;
        }
        //彈出隊頭,更新隊頭指針
        Node<E> temp = this.head; //獲取隊頭引用
        this.head = this.head.next; //更新隊頭指針
        temp.next = null; //釋放原隊頭節點引用
        return temp.data;
    }

    /**
     * 打印數據
     */
    public void display(){
        for (int i = 0; i < this.size; i++) {
            if(this.head == null){
                return;
            }
            System.out.println(this.head.data);
            this.head = this.head.next;
        }
    }
}
public class LinkedTest {
    public static void main(String[] args) {
        QueueLinked queueLinked = new QueueLinked();
        queueLinked.push("1");
        queueLinked.push(2);
        queueLinked.push("5");
        System.out.println(queueLinked.pop());
        queueLinked.push(7);
        queueLinked.push("9");
        System.out.println(queueLinked.pop());
        System.out.println(queueLinked.pop());
        queueLinked.display();
    }
}

運行結果:

總結

隊列的數據結構,簡單的瞭解一下,寫底層C代碼纔會使用上,平時用不上可惜。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章