欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

java中如何實現(xiàn)隊列數(shù)組和鏈表

這篇文章將為大家詳細講解有關java中如何實現(xiàn)隊列數(shù)組和鏈表,文章內(nèi)容質(zhì)量較高,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

龍巖網(wǎng)站建設公司成都創(chuàng)新互聯(lián),龍巖網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為龍巖近1000家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的龍巖做網(wǎng)站的公司定做!

隊列的介紹

隊列是一種先進先出(FIFO)的線性的數(shù)據(jù)結構,隊列的主要操作為入隊和出隊。

隊頭:隊列的出口端,隊尾:隊列的入口端,通常在數(shù)組中表示為最后入隊元素的下一個位置。

在用數(shù)組實現(xiàn)時,注意:若隊頭不斷有元素出隊,那么隊列的可用空間就會變小,所以我們通常用循環(huán)隊列來實現(xiàn),此時隊尾也可能出現(xiàn)在隊頭的前面。

隊列的數(shù)組實現(xiàn)

隊列的數(shù)組實現(xiàn)這里的隊列一般都是循環(huán)隊列!

特別注意:

(1)隊列滿時的判斷條件為(隊尾下標+1) % 數(shù)組長度 = 隊頭下標

(2)隊尾指針指向的位置空出一位,因此隊列最大容量比數(shù)組長度小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++){
//一直在循環(huán)輸出,嚴重錯誤!不能把取模判斷語句寫在條件里面!
//            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();
    }
 
}

隊列的鏈表實現(xiàn)

隊列用鏈表實現(xiàn)時,用頭指針指向隊列的第一個節(jié)點,用尾指針指向隊列的最后一個節(jié)點。

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;//隊列中實際元素的個數(shù)
    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)//隊列中只有一個節(jié)點
            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)網(wǎng)絡爬蟲實現(xiàn)網(wǎng)站抓取,就是把待抓取的網(wǎng)站URL存入隊列中,再按照存入隊列的順序來依次抓取和解析。

以上就是java中實現(xiàn)隊列數(shù)組和鏈表的方法,看完之后是否有所收獲呢?如果想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊,感謝各位的閱讀。

分享名稱:java中如何實現(xiàn)隊列數(shù)組和鏈表
網(wǎng)站URL:http://aaarwkj.com/article44/pjsohe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、App開發(fā)、企業(yè)建站、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設計、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設
久久综合给合综合久久| 亚洲欧美天堂一区二区| 国产日韩传媒在线观看| 男女做爰高清无遮挡免费| 亚洲女人下体毛茸茸视频| 久久婷婷综合激情亚洲| 欧美日韩一区二区三区色拉拉| 精品一区二区三区乱码中文| 又黄又湿又刺激中文字幕| 亚洲精品在线观看午夜福利| 日本女优中文字幕久久| 久草午夜福利视频免费观看| 国产在线精品91国自产拍| 一区二区三区视频免费观看| 国产成人精品高清国产三级| 国产又粗又长在线视频| 亚洲品质一区二区三区| 欧洲一区二区三区黄色| 欧美日韩一区中文字幕 | 成人黄色18免费网站| 亚洲精品在线观看毛片| 日本不卡在线观看欧美精品 | 亚洲欧美日韩专区一区| 一区二区三区视频免费观看| 欧美日韩在线精品1区2区| 国产精品一级片免费看| 精品亚洲一区二区三区四区| 国产精品高清另类一区二区三区| 99热这里只有精品最新| 夜福利国产视频大屁股| 中文字幕一区侵犯人妻| 日本亚洲一区二区在线| 亚洲成熟中老妇女视频| 日韩激情小视频在线观看| 天天操夜夜操狠狠操91| 欧美精品一区二区三区色| 在线观看国产自拍精品| 日本一区二区不卡高清| 国产三级无遮挡在线观看| 日韩av在线不卡一区二区| 欧美日韩欧美日韩一区二区|