首頁  >  文章  >  Java  >  java中關於隊列的數組和鍊錶實現

java中關於隊列的數組和鍊錶實現

王林
王林轉載
2019-11-28 13:44:591768瀏覽

java中關於隊列的數組和鍊錶實現

佇列的介紹

佇列是一種先進先出(FIFO)的線性的資料結構,佇列的主要操作為入隊和出隊。

隊頭:佇列的出口端,隊尾:佇列的入口端,通常在陣列中表示為最後入隊元素的下一個位置。

在用數組實現時,注意:若隊頭不斷有元素出隊,那麼隊列的可用空間就會變小,所以我們通常用循環隊列來實現,此時隊尾也可能出現在隊頭的前方。

相關學習影片教學建議:java學習

#佇列的陣列實作 

##佇列的陣列實作這裡的隊列一般都是循環隊列!

特別注意:

(1)佇列滿時的判斷條件為(隊尾下標1) % 陣列長度= 隊頭下標

(2)隊尾指標指向的位置空出一位,因此佇列最大容量比陣列長度小1。

public class MyQueue {
    private int[] array;
    private int front;
    private int rear;
    public MyQueue(int capacity){
        array = new int[capacity];
    }
 
    /*
    入队时,只需判断队列是否已满,若队列已满,则抛出异常,其他情况(包括队列为空)都正常插入
     */
    public void enQueue(int data) throws Exception{
        if((rear+1) % array.length  == front)
            throw new Exception("队列已满,不能入队!");
        array[rear] = data;
        rear = (rear+1) % array.length;
    }
 
    /*
    出队时,判断队列是否为空,若队列为空,抛出异常
     */
    public int deQueue() throws Exception{
        if(front == rear)
            throw new Exception("队列为空,不能出队!");
        int temp = array[front];
        front = (front+1) % array.length;
        return temp;
    }
 
//    public void output(){
//        for(int i = front; ((i+1) % array.length) <= rear; i++){
//一直在循环输出,严重错误!不能把取模判断语句写在条件里面!
//            i %= array.length;
//            System.out.println(array[i]);
//        }
//    }
 
    public void output(){
        for(int i = front; i != rear; i = (i+1) % array.length){
            System.out.println(array[i]);
        }
    }
    public static void main(String[] args) throws Exception{
        MyQueue myQueue = new MyQueue(5);//长度为5的队列只能插入4个元素
        myQueue.enQueue(1);
        myQueue.enQueue(3);
        myQueue.enQueue(2);
        myQueue.enQueue(4);
        myQueue.deQueue();
        myQueue.deQueue();
        myQueue.enQueue(5);
        myQueue.enQueue(6);
        myQueue.output();
    }
 
}

佇列的鍊錶實作

佇列用鍊錶實作時,用頭指標指向佇列的第一個節點,用尾指標指向隊列的最後一個節點。

public class MyQueue_LinkList {
    private static class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
        }
    }
 
    private Node front;
    private Node rear;
    private int size;//队列中实际元素的个数
    private int maxsize;
 
    public MyQueue_LinkList(int capacity){
        maxsize = capacity;
    }
 
    public void enQueue(int data) throws Exception{
        if(size >= maxsize)
            throw new Exception("队列已满,无法入队");
        Node insertedNode = new Node(data);
        if(size == 0){
            front = insertedNode;
            rear = insertedNode;
        }
        else{
            rear.next = insertedNode;
            rear = insertedNode;
        }
        size++;
    }
 
    public int deQueue() throws Exception{
        if(front == null)
            throw new Exception("队列为空,无法出队!");
        int temp;
        if(front == rear)//队列中只有一个节点
            rear = null;
        temp = front.data;
        front = front.next;
        size--;
        return temp;
    }
 
    public void output(){
        Node temp = front;
        for(int i = 0 ; i < size; i++){
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
 
    public static void main(String[] args) throws Exception{
        MyQueue_LinkList myQueue_linkList = new MyQueue_LinkList(5);
        myQueue_linkList.enQueue(1);
        myQueue_linkList.enQueue(3);
        myQueue_linkList.enQueue(2);
        myQueue_linkList.deQueue();
        myQueue_linkList.deQueue();
        myQueue_linkList.enQueue(5);
        myQueue_linkList.enQueue(7);
        myQueue_linkList.output();
 
    }
}

佇列的應用程式場景

(1)銀行排隊,先來先服務。

(2)多執行緒中,爭奪公平鎖的等待佇列,就是依照存取順序來決定執行緒在佇列中的次序的。

(3)網路爬蟲實作網站抓取,就是把待抓取的網站URL存入佇列中,再依照存入佇列的順序依序抓取和解析。

相關文章教學推薦:

java入門教學#

以上是java中關於隊列的數組和鍊錶實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除