黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java實現隊列的例子

Java實現隊列的例子

更新時間:2022-04-07 12:27:50 來源:動力節點 瀏覽2177次

在本文中,動力節點小編會給大家介紹Java實現隊列。隊列是遵循 FIFO(先進先出)原則的線性數據結構。這意味著首先插入的對象將是第一個出來的對象,然后是下一個插入的對象。

隊列支持以下核心操作:

入隊:在隊列的尾部插入一個項目。

出隊:從隊列的前面移除對象并返回它,從而將隊列大小減一。

Peek:返回隊列前面的對象而不刪除它。

IsEmpty:測試隊列是否為空。

大小:返回隊列中存在的元素總數。

使用數組的隊列實現:

// A class to represent a queue
class Queue
{
    private int[] arr;      // array to store queue elements
    private int front;      // front points to the front element in the queue
    private int rear;       // rear points to the last element in the queue
    private int capacity;   // maximum capacity of the queue
    private int count;      // current size of the queue 
    // Constructor to initialize a queue
    Queue(int size)
    {
        arr = new int[size];
        capacity = size;
        front = 0;
        rear = -1;
        count = 0;
    } 
    // Utility function to dequeue the front element
    public int dequeue()
    {
        // check for queue underflow
        if (isEmpty())
        {
            System.out.println("Underflow\nProgram Terminated");
            System.exit(-1);
        } 
        int x = arr[front]; 
        System.out.println("Removing " + x); 
        front = (front + 1) % capacity;
        count--; 
        return x;
    } 
    // Utility function to add an item to the queue
    public void enqueue(int item)
    {
        // check for queue overflow
        if (isFull())
        {
            System.out.println("Overflow\nProgram Terminated");
            System.exit(-1);
        } 
        System.out.println("Inserting " + item); 
        rear = (rear + 1) % capacity;
        arr[rear] = item;
        count++;
    } 
    // Utility function to return the front element of the queue
    public int peek()
    {
        if (isEmpty())
        {
            System.out.println("Underflow\nProgram Terminated");
            System.exit(-1);
        }
        return arr[front];
    } 
    // Utility function to return the size of the queue
    public int size() {
        return count;
    } 
    // Utility function to check if the queue is empty or not
    public boolean isEmpty() {
        return (size() == 0);
    } 
    // Utility function to check if the queue is full or not
    public boolean isFull() {
        return (size() == capacity);
    }
} 
class Main
{
    public static void main (String[] args)
    {
        // create a queue of capacity 5
        Queue q = new Queue(5); 
        q.enqueue(1);
        q.enqueue(2);
        q.enqueue(3); 
        System.out.println("The front element is " + q.peek());
        q.dequeue();
        System.out.println("The front element is " + q.peek()); 
        System.out.println("The queue size is " + q.size()); 
        q.dequeue();
        q.dequeue(); 
        if (q.isEmpty()) {
            System.out.println("The queue is empty");
        }
        else {
            System.out.println("The queue is not empty");
        }
    }
}

輸出:

Inserting 1
Inserting 2
Inserting 3
前面的元素是 1
移除 1
前面的元素是 2
隊列大小是 2
移除 2
移除 3
隊列是空的

enqueue()、dequeue()、peek()和函數 的時間復雜度是常數isEmpty(),size()即O(1)。

使用Queue界面:

Java的庫還包含指定隊列操作的Queue接口。以下是使用該的Queue接口示例:LinkedList

import java.util.LinkedList;
import java.util.Queue; 
class Main
{
    public static void main(String[] args)
    {
        Queue<String> queue = new LinkedList<String>(); 
        queue.add("A");     // Insert `A` into the queue
        queue.add("B");     // Insert `B` into the queue
        queue.add("C");     // Insert `C` into the queue
        queue.add("D");     // Insert `D` into the queue 
        // Prints the front of the queue (`A`)
        System.out.println("The front element is " + queue.peek()); 
        queue.remove();     // removing the front element (`A`)
        queue.remove();     // removing the front element (`B`) 
        // Prints the front of the queue (`C`)
        System.out.println("The front element is " + queue.peek()); 
        // Returns the total number of elements present in the queue
        System.out.println("The queue size is " + queue.size()); 
        // check if the queue is empty
        if (queue.isEmpty()) {
            System.out.println("The queue is empty");
        }
        else {
            System.out.println("The queue is not empty");
        }
    }
}

輸出:

前面元素是 A
前面元素是 C
隊列大小是 2
隊列不為空

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: a免费毛片在线播放 | 国模在线视频一区二区三区 | 午夜性色吃奶添下面69影院 | 日韩在线 | 中文 | 免费一级毛片在线播放放视频 | 精品人人 | 91全国探花精品正在播放 | 国产69精品久久久久777 | 综合自拍亚洲综合图不卡区 | 国产字幕制服中文在线 | 欧美人与禽杂交大片 | 一个人看的www片免费中文 | 天天爱天天做色综合 | 高清午夜看片a福利在线观看琪琪 | 狠狠色网 | 中文字幕一精品亚洲无线一区 | 性大特级毛片视频 | 成人免费观看视频高清视频 | 最近2019中文字幕mv免费看 | 日本一区欧美 | 亚洲国产成人在线视频 | 日日摸夜夜添夜夜添影院视频 | 欧美日本黄色片 | 国产小视频在线高清播放 | 一本无线乱码不卡一二三四 | 久久一区二区精品 | 欧美黑人巨大3dvideo | 中文字幕日韩专区精品系列 | 波多野结衣精品一区二区三区 | 日本三级在在线观看 | 国产精品国产精品国产三级普 | 中国大陆高清aⅴ毛片 | 国产黄色大片网站 | a高清免费毛片久久 | 欧美伊人久久综合网 | 日皮视频免费看 | 久久精品免费 | 成人毛片免费观看视频 | 黄色搞鸡网站 | 亚洲天堂va | 久久亚洲成a人片 |