大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java實(shí)現(xiàn)隊(duì)列的例子

Java實(shí)現(xiàn)隊(duì)列的例子

更新時(shí)間:2022-04-07 12:27:50 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2155次

在本文中,動(dòng)力節(jié)點(diǎn)小編會(huì)給大家介紹Java實(shí)現(xiàn)隊(duì)列。隊(duì)列是遵循 FIFO(先進(jìn)先出)原則的線性數(shù)據(jù)結(jié)構(gòu)。這意味著首先插入的對(duì)象將是第一個(gè)出來的對(duì)象,然后是下一個(gè)插入的對(duì)象。

隊(duì)列支持以下核心操作:

入隊(duì):在隊(duì)列的尾部插入一個(gè)項(xiàng)目。

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

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

IsEmpty:測試隊(duì)列是否為空。

大小:返回隊(duì)列中存在的元素總數(shù)。

使用數(shù)組的隊(duì)列實(shí)現(xiàn):

// 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
隊(duì)列大小是 2
移除 2
移除 3
隊(duì)列是空的

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

使用Queue界面:

Java的庫還包含指定隊(duì)列操作的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
隊(duì)列大小是 2
隊(duì)列不為空

 

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 草莓视频一区二区精品 | 99热久久这里只有精品6国产网 | 丁香婷婷网| 日日噜噜夜夜狠狠tv视频免费 | 中文字幕一区久久久久 | 国产精品第一区亚洲精品 | 尹人香蕉久久99天天拍欧美p7 | 五月月色开心婷婷久久合 | 久久精品国产亚洲妲己影院 | 成人午夜爱爱爱爱爱 | 老色鬼a∨在线视频在线观看 | 午夜欧美在线 | 香蕉视频在线观看免费 | 日本xxxx色视频在线观看免 | 欧美a视频在线观看 | 97免费看| 欧美精品 日韩 | 一区二区三区免费视频 www | 亚洲天堂欧美 | 欧美激情久久久久久久久 | 伊在人亚洲香蕉精品区麻豆 | 国产香蕉在线视频 | 久久综合九色综合97婷婷女人 | 亚洲欧美日韩精品中文乱码 | 欧美色视频日本片高清在线观看 | 国产福利一区二区三区在线视频 | 国产视频1 | 久久99久久精品国产99热 | 性欧美日韩 | 日日夜夜精品视频 | 成人毛片在线视频 | 青娱乐国产在线视频 | 天天干夜夜操 | 日韩成人免费一级毛片 | 一级免费毛片 | 精品亚洲永久免费精品 | 天天爽夜夜爽免费看 | 久操小视频| 香蕉视频国产 | 成人在色线视频在线观看免费大全 | 午夜探花 |