Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(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)
- 判斷隊(duì)列是否為空: 當(dāng)頭指針 Q.front == 尾指針 Q.rear, 隊(duì)列為空
- 判斷隊(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)文章
SpringBoot整合Dubbo框架,實(shí)現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用
Dubbo是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠(yuǎn)程方法調(diào)用,智能容錯(cuò)和負(fù)載均衡,以及服務(wù)自動(dòng)注冊和發(fā)現(xiàn)。今天就來看下SpringBoot整合Dubbo框架的步驟2021-06-06
SpringBoot過濾敏感詞的兩種實(shí)現(xiàn)方式
Spring Boot本身并不直接提供過濾敏感詞的功能,但你可以使用第三方庫或者自定義過濾器來實(shí)現(xiàn)這個(gè)需求,所以本文給大家介紹了SpringBoot過濾敏感詞的兩種實(shí)現(xiàn)方式,感興趣的朋友可以參考下2024-06-06
MybatisPlus實(shí)現(xiàn)邏輯刪除功能
這篇文章主要介紹了MybatisPlus實(shí)現(xiàn)邏輯刪除功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù)功能
這篇文章主要介紹了Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
解決Servlet4.0版本使用注解設(shè)置url但無法訪問的問題
在學(xué)習(xí)servlet過程中,使用web.xml文件配置servlet可以正常訪問,但使用WebServlet注解時(shí)出現(xiàn)404錯(cuò)誤,解決方法是在web.xml文件中將metadata-complete屬性改為false,啟動(dòng)標(biāo)注支持,然而該方法對我無效,最后通過重建項(xiàng)目和手動(dòng)將新建的項(xiàng)目添加到tomcat服務(wù)器解決問題2024-10-10
SpringBoot實(shí)現(xiàn)子類的反序列化示例代碼
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)子類的反序列化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
教你通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引
大家都知道B+Tree是從二叉樹演化而來,在這之前我們來先了解二叉樹、平衡二叉樹、平衡多叉樹,這篇文章主要介紹了通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引,需要的朋友可以參考下2022-01-01
IntelliJ IDEA 安裝教程2019.09.23(最新版)
本文通過圖文并茂的形式給大家介紹了IntelliJ IDEA 安裝教程2019.09.23最新版,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10

