欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)

 更新時(shí)間:2021年12月14日 11:13:37   作者:我是小白呀  
循環(huán)隊(duì)列 (Circular Queue) 是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問題。本文將帶大家詳細(xì)了解循環(huán)隊(duì)列如何實(shí)現(xiàn),需要的朋友可以參考一下

概述

從今天開始, 小白我將帶大家開啟 Jave 數(shù)據(jù)結(jié)構(gòu) & 算法的新篇章.

循環(huán)隊(duì)列

循環(huán)隊(duì)列 (Circular Queue) 是一種特殊的隊(duì)列. 循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位 (復(fù)雜度為 O(n)) 的問題.

循環(huán)隊(duì)列的底層依然是數(shù)組, 不過增加了指向頭和尾的指針.

循環(huán)隊(duì)列實(shí)現(xiàn)

  1. 判斷隊(duì)列是否為空: 當(dāng)頭指針 Q.front == 尾指針 Q.rear, 隊(duì)列為空
  2. 判斷隊(duì)列是否為滿: 當(dāng)頭指針 Q.front == 尾指針 Q.rear + 1) % Maxsize, 隊(duì)列為滿

改變隊(duì)列大小

// 改變隊(duì)列大小
private void resize(int capacity) {
	// 定義新數(shù)組
    E[] newData = (E[]) new Object[capacity + 1];
    
    // 轉(zhuǎn)移數(shù)組元素
    for (int i = 0; i < size; i++) {
        newData[i] = data[(i +front) % data.length];
    }

    // 更新
    data = newData;
    front = 0;
    rear = size;
}

enqueue 方法

// 入隊(duì)
public void enqueue(E e) {

    // 判斷是否為滿
    if((rear + 1) % data.length == front) {
        resize(getCapacity() * 2);
    }

    // 隊(duì)伍加入
    data[rear] = e;
    rear = (rear + 1) % data.length;
    size++;
}

dequeue 方法

// 出隊(duì)
public E dequeue() {

    // 判斷是否為空
    if(isEmpty()) {
        throw new RuntimeException("隊(duì)列為空");
    }

    // 取出第一個(gè)元素
    E element = data[front];
    data[front] = null;

    // 移動(dòng)頭指針
    front = (front + 1) % data.length;

    // 數(shù)組大小-1
    size--;

    // 判斷是否需要縮小
    if(size==getCapacity() / 4 && getCapacity() / 2 != 0) {
        resize(getCapacity() / 2);
    }

    return element;
}

main

// 主函數(shù)
public static void main(String[] args) {

    // 創(chuàng)建循環(huán)隊(duì)列
    CircularQueue<Integer> queue = new CircularQueue<>(5);

    // 入隊(duì)
    for (int i = 0; i < 8; i++) {
        queue.enqueue(i);
        System.out.println(queue);
    }

    // 出隊(duì)
    for (int i = 0; i < 8; i++) {
        queue.dequeue();
        System.out.println(queue);
    }
}

輸出結(jié)果:

CircularQueue{data=[0, null, null, null, null, null], front=0, rear=1, size=1}

CircularQueue{data=[0, 1, null, null, null, null], front=0, rear=2, size=2}

CircularQueue{data=[0, 1, 2, null, null, null], front=0, rear=3, size=3}

CircularQueue{data=[0, 1, 2, 3, null, null], front=0, rear=4, size=4}

CircularQueue{data=[0, 1, 2, 3, 4, null], front=0, rear=5, size=5}

CircularQueue{data=[0, 1, 2, 3, 4, 5, null, null, null, null, null], front=0, rear=6, size=6}

CircularQueue{data=[0, 1, 2, 3, 4, 5, 6, null, null, null, null], front=0, rear=7, size=7}

CircularQueue{data=[0, 1, 2, 3, 4, 5, 6, 7, null, null, null], front=0, rear=8, size=8}

CircularQueue{data=[null, 1, 2, 3, 4, 5, 6, 7, null, null, null], front=1, rear=8, size=7}

CircularQueue{data=[null, null, 2, 3, 4, 5, 6, 7, null, null, null], front=2, rear=8, size=6}

CircularQueue{data=[null, null, null, 3, 4, 5, 6, 7, null, null, null], front=3, rear=8, size=5}

CircularQueue{data=[null, null, null, null, 4, 5, 6, 7, null, null, null], front=4, rear=8, size=4}

CircularQueue{data=[null, null, null, null, null, 5, 6, 7, null, null, null], front=5, rear=8, size=3}

CircularQueue{data=[6, 7, null, null, null, null], front=0, rear=2, size=2}

CircularQueue{data=[7, null, null], front=0, rear=1, size=1}

CircularQueue{data=[null, null], front=0, rear=0, size=0}

完整代碼?

import java.util.ArrayList;
import java.util.Arrays;

public class CircularQueue<E> {

    private E[] data;
    private int front, rear;
    private int size;

    // 無參構(gòu)造
    public CircularQueue() {
        this(10);
    }

    // 有參構(gòu)造
    public CircularQueue(int capacity) {
       data = (E[]) new Object[capacity + 1];
       front = rear = size = 0;
    }

    // 入隊(duì)
    public void enqueue(E e) {

        // 判斷是否為滿
        if((rear + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }

        // 隊(duì)伍加入
        data[rear] = e;
        rear = (rear + 1) % data.length;
        size++;
    }

    // 出隊(duì)
    public E dequeue() {

        // 判斷是否為空
        if(isEmpty()) {
            throw new RuntimeException("隊(duì)列為空");
        }

        // 取出第一個(gè)元素
        E element = data[front];
        data[front] = null;

        // 移動(dòng)頭指針
        front = (front + 1) % data.length;

        // 數(shù)組大小-1
        size--;

        // 判斷是否需要縮小
        if(size==getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }

        return element;
    }

    // 獲取隊(duì)列大小
    public int getSize() {
        return size;
    }

    // 獲取隊(duì)列容量
    public int getCapacity() {
        return data.length - 1;
    }

    // 隊(duì)列是否為空
    public boolean isEmpty() {
        return front == rear;
    }

    // 改變隊(duì)列大小
    private void resize(int capacity) {

        // 創(chuàng)建新數(shù)組
        E[] newData = (E[]) new Object[capacity + 1];

        // 轉(zhuǎn)移數(shù)組元素
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i +front) % data.length];
        }

        // 更新
        data = newData;
        front = 0;
        rear = size;
    }

    @Override
    public String toString() {
        return "CircularQueue{" +
                "data=" + Arrays.toString(data) +
                ", front=" + front +
                ", rear=" + rear +
                ", size=" + size +
                '}';
    }

    // 主函數(shù)
    public static void main(String[] args) {

        // 創(chuàng)建循環(huán)隊(duì)列
        CircularQueue<Integer> queue = new CircularQueue<>(5);

        // 入隊(duì)
        for (int i = 0; i < 8; i++) {
            queue.enqueue(i);
            System.out.println(queue);
        }

        // 出隊(duì)
        for (int i = 0; i < 8; i++) {
            queue.dequeue();
            System.out.println(queue);
        }
    }
} 

到此這篇關(guān)于Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java數(shù)據(jù)結(jié)構(gòu) 循環(huán)隊(duì)列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JVM 心得 OOM時(shí)的堆信息獲取方法與分析

    JVM 心得 OOM時(shí)的堆信息獲取方法與分析

    下面小編就為大家?guī)硪黄狫VM 心得 OOM時(shí)的堆信息獲取方法與分析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • SpringCloud中的Feign詳解

    SpringCloud中的Feign詳解

    這篇文章主要介紹了SpringCloud中的Feign詳解,Feign是一個(gè)聲明式的Web Service客戶端,以Java接口注解的方式調(diào)用Http請求,同時(shí)Feign整合了Ribbon和Hystrix,實(shí)現(xiàn)負(fù)載均衡與容斷功能,需要的朋友可以參考下
    2023-09-09
  • Spring Boot console log 格式自定義方式

    Spring Boot console log 格式自定義方式

    這篇文章主要介紹了Spring Boot console log 格式自定義方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能

    如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能

    最近在研究mybatis,然后就去找簡化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過對比發(fā)現(xiàn)還是mybatis-plus比較好,下面這篇文章主要給大家介紹了關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Maven 打包項(xiàng)目到私服 (deploy)的配置方法

    Maven 打包項(xiàng)目到私服 (deploy)的配置方法

    這篇文章主要介紹了Maven 打包項(xiàng)目到私服 (deploy)的配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java元注解Retention代碼示例介紹

    Java元注解Retention代碼示例介紹

    注解@Retention可以用來修飾注解,是注解的注解,稱為元注解。Retention注解有一個(gè)屬性value,是RetentionPolicy類型的,Enum?RetentionPolicy是一個(gè)枚舉類型,這個(gè)枚舉決定了Retention注解應(yīng)該如何去保持,也可理解為Rentention?搭配?RententionPolicy使用
    2022-08-08
  • 一起來了解Java的File類和IO流

    一起來了解Java的File類和IO流

    這篇文章主要為大家詳細(xì)介紹了Java?File類和IO流,在Java學(xué)習(xí)中,file類與io流是非常重要的部分,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java異常處理中同時(shí)有finally和return語句的執(zhí)行問題

    Java異常處理中同時(shí)有finally和return語句的執(zhí)行問題

    這篇文章主要介紹了Java異常處理中同時(shí)有finally和return語句的執(zhí)行問題,首先確定的是一般finally語句都會(huì)被執(zhí)行...然后,需要的朋友可以參考下
    2015-11-11
  • 詳解spring cloud config實(shí)現(xiàn)datasource的熱部署

    詳解spring cloud config實(shí)現(xiàn)datasource的熱部署

    這篇文章主要介紹了詳解spring cloud config實(shí)現(xiàn)datasource的熱部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動(dòng)策略

    logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動(dòng)策略

    這篇文章主要介紹了FixedWindowRollingPolicy根據(jù)logback 固定窗口算法重命名文件滾動(dòng)策略源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評論