1. 程式人生 > 實用技巧 >《Java資料結構》Java Queue佇列

《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程式碼才會使用上,平時用不上可惜。