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)文章
Spring Boot console log 格式自定義方式
這篇文章主要介紹了Spring Boot console log 格式自定義方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07如何使用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-06Maven 打包項(xiàng)目到私服 (deploy)的配置方法
這篇文章主要介紹了Maven 打包項(xiàng)目到私服 (deploy)的配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java異常處理中同時(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的熱部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動(dòng)策略
這篇文章主要介紹了FixedWindowRollingPolicy根據(jù)logback 固定窗口算法重命名文件滾動(dòng)策略源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11