Java循環(huán)隊列與非循環(huán)隊列的區(qū)別總結(jié)
非循環(huán)循環(huán)隊列
- 判滿:(rear+1) % maxsize == front
- 判空:front == rear
- 隊列元素個數(shù):rear = (rear + maxsize - front) % maxsize
- front指針移動方式:front = (front + 1) % maxsize
- rear指針移動方式:rear= (rear+ 1) % maxsize
import java.awt.Font;
import java.util.Scanner;
import javax.management.RuntimeErrorException;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//創(chuàng)建隊列
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):顯示隊列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加數(shù)據(jù)到隊列");
System.out.println("g(get):從隊列取出數(shù)據(jù)");
System.out.println("h(head):查看隊列頭的數(shù)據(jù)");
key = scanner.next().charAt(0);
switch (key) {
case 's':
circleArrayQueue.showQueue();
break;
case 'e':
circleArrayQueue.showQueue();
break;
case 'a':
System.out.println("輸入一個數(shù)");
int value = scanner.nextInt();
circleArrayQueue.addQueue(value);
break;
case 'g':
try {
int res = circleArrayQueue.getQueue();
System.out.printf("取出的數(shù)據(jù)是%d\n",res);
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = circleArrayQueue.headQueue();
System.out.printf("隊列頭的數(shù)據(jù)是%d\n",res);
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出");
}
}
//隊列
class CircleArrayQueue{
private int maxsize;
private int front;
private int rear;
private int[] arr;
//構(gòu)造器
public CircleArrayQueue(int arrmaxsize) {
maxsize = arrmaxsize;
front = 0;
rear = 0;
arr = new int[maxsize];
}
//判滿
public boolean isFull() {
return (rear+1)%maxsize == front;
}
//判空
public boolean isEmpty() {
return rear == front;
}
//入隊
public void addQueue(int n) {
if(isFull()) {
System.out.println("隊列已滿,不能再添加!");
return;
}
//添加數(shù)據(jù)
arr[rear] = n;
//rear后移
rear = (rear + 1) % maxsize;
}
//出隊
public int getQueue() {
if(isEmpty()) {
throw new RuntimeException("隊列為空,不可取出元素!");
}
//取值
int value = arr[front];
//front后移
front = (front + 1)%maxsize;
return value;
}
//遍歷
public void showQueue() {
if(isEmpty()) {
System.out.println("隊列為空!");
return;
}
for(int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]);
}
}
//求隊列有效數(shù)據(jù)的個數(shù)
public int size() {
return (rear + maxsize - front) % maxsize;
}
//顯示隊頭元素
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("隊列為空,不可取出元素!");
}
return arr[front];
}
結(jié)果示意圖



循環(huán)隊列
- 判滿:(rear+1) % maxsize == front
- 判空:front == rear
- 隊列元素個數(shù):rear = (rear + maxsize - front) % maxsize
- front 指針移動方式:front = (front + 1) % maxsizer
- ear指針移動方式:rear= (rear+ 1) % maxsize
import java.awt.Font;
import java.util.Scanner;
import javax.management.RuntimeErrorException;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//創(chuàng)建隊列
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):顯示隊列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加數(shù)據(jù)到隊列");
System.out.println("g(get):從隊列取出數(shù)據(jù)");
System.out.println("h(head):查看隊列頭的數(shù)據(jù)");
key = scanner.next().charAt(0);
switch (key) {
case 's':
circleArrayQueue.showQueue();
break;
case 'e':
circleArrayQueue.showQueue();
break;
case 'a':
System.out.println("輸入一個數(shù)");
int value = scanner.nextInt();
circleArrayQueue.addQueue(value);
break;
case 'g':
try {
int res = circleArrayQueue.getQueue();
System.out.printf("取出的數(shù)據(jù)是%d\n",res);
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = circleArrayQueue.headQueue();
System.out.printf("隊列頭的數(shù)據(jù)是%d\n",res);
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出");
}
}
//隊列
class CircleArrayQueue{
private int maxsize;
private int front;
private int rear;
private int[] arr;
//構(gòu)造器
public CircleArrayQueue(int arrmaxsize) {
maxsize = arrmaxsize;
front = 0;
rear = 0;
arr = new int[maxsize];
}
//判滿
public boolean isFull() {
return (rear+1)%maxsize == front;
}
//判空
public boolean isEmpty() {
return rear == front;
}
//入隊
public void addQueue(int n) {
if(isFull()) {
System.out.println("隊列已滿,不能再添加!");
return;
}
//添加數(shù)據(jù)
arr[rear] = n;
//rear后移
rear = (rear + 1) % maxsize;
}
//出隊
public int getQueue() {
if(isEmpty()) {
throw new RuntimeException("隊列為空,不可取出元素!");
}
//取值
int value = arr[front];
//front后移
front = (front + 1)%maxsize;
return value;
}
//遍歷
public void showQueue() {
if(isEmpty()) {
System.out.println("隊列為空!");
return;
}
for(int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]);
}
}
//求隊列有效數(shù)據(jù)的個數(shù)
public int size() {
return (rear + maxsize - front) % maxsize;
}
//顯示隊頭元素
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("隊列為空,不可取出元素!");
}
return arr[front];
}
}
結(jié)果示意圖



到此這篇關(guān)于Java循環(huán)隊列與非循環(huán)隊列的區(qū)別總結(jié)的文章就介紹到這了,更多相關(guān)Java循環(huán)隊列與非循環(huán)隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java數(shù)組實現(xiàn)循環(huán)隊列示例介紹
- Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊列的實現(xiàn)
- Java代碼實現(xiàn)循環(huán)隊列的示例代碼
- java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):順序隊列和循環(huán)隊列
- Java動態(tài)循環(huán)隊列是如何實現(xiàn)的
- Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊列
- Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列
- Java循環(huán)隊列原理與用法詳解
- Java?循環(huán)隊列/環(huán)形隊列的實現(xiàn)流程
相關(guān)文章
mybatis typeAliases 給實體類起別名的方法
這篇文章主要介紹了mybatis typeAliases 給實體類起別名,本文給大家分享兩種用法,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java反射中java.beans包學(xué)習(xí)總結(jié)
本篇文章通過學(xué)習(xí)Java反射中java.beans包,吧知識點(diǎn)做了總結(jié),并把相關(guān)內(nèi)容做了關(guān)聯(lián),對此有需要的朋友可以學(xué)習(xí)參考下。2018-02-02
Spring創(chuàng)建bean的幾種方式及使用場景
本文主要介紹了Spring創(chuàng)建bean的幾種方式及使用場景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像)
這篇文章主要介紹了maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java中Timer的schedule()方法參數(shù)詳解
今天小編就為大家分享一篇關(guān)于Java中Timer的schedule()方法參數(shù)詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
java面向?qū)ο笤O(shè)計原則之迪米特法則分析詳解
這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計原則之迪米特法則的示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,學(xué)有所得2021-10-10
解決gateway報netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemor
這篇文章主要介紹了解決gateway報netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

