java數(shù)組實現(xiàn)隊列及環(huán)形隊列實現(xiàn)過程解析
這篇文章主要介紹了java數(shù)組實現(xiàn)隊列及環(huán)形隊列實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

代碼內(nèi)容
ArrayQueue---用數(shù)組實現(xiàn)隊列
package com.structure;
import java.util.Scanner;
/**
* @auther::9527
* @Description: 數(shù)組模擬隊列
* @program: jstl2
* @create: 2019-10-05 08:58
*/
public class ArrayQueueDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//測試
ArrayQueue queue = new ArrayQueue(3);
char key = ' '; //接受用戶輸入
boolean loop = true; //循環(huán)終止判斷器
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ù)");
System.out.println("按提示輸入......");
key = scanner.next().charAt(0); //接收數(shù)據(jù)
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("請輸入一個數(shù)");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getQueue();
System.out.printf("取出的數(shù)是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("隊列頭部的數(shù)是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
System.out.println("...輸入不合法,請重新輸入...");
break;
}
}
System.out.println("程序退出....");
}
}
//使用數(shù)組模擬隊列--編寫一個ArrayQueue
class ArrayQueue {
private int maxSize; //數(shù)組的最大容量
private int front; //隊列頭
private int rear; //隊列尾
private int[] arr; //存放數(shù)據(jù)的數(shù)組,模擬隊列.
//創(chuàng)建隊列的構(gòu)造器
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;
rear = -1;
}
//判斷隊列是否已經(jīng)滿了
public boolean isFull() {
return rear == maxSize - 1;
}
//判斷隊列是否為空
public boolean isEmpty() {
return rear == front;
}
//添加數(shù)據(jù)到隊列
public void addQueue(int n) {
//判斷隊列是否已滿
if (isFull()) {
System.out.println("隊列已滿,不能添加");
return;
}
rear++; //指針后移
arr[rear] = n;
}
//數(shù)據(jù)出隊列
public int getQueue() {
//判斷隊列是否為空
if (isEmpty()) {
throw new RuntimeException("隊列為空,不能取數(shù)據(jù)");
}
front++; //頭部指針后移
return arr[front];
}
//顯示隊列的所有數(shù)據(jù)
public void showQueue() {
if (isEmpty()) {
System.out.println("隊列為空,沒有數(shù)據(jù)");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
//顯示隊列的頭部數(shù)據(jù),這個不是取出數(shù)據(jù)
public int headQueue() {
//判斷是否為空
if (isEmpty()) {
System.out.println("隊列為空,沒有數(shù)據(jù)....");
throw new RuntimeException("隊列為空,沒有數(shù)據(jù)....");
}
return arr[front + 1];
}
}
改進(jìn)版---環(huán)形隊列:

環(huán)形隊列代碼
package com.structure;
import java.util.Scanner;
/**
* @auther::9527
* @Description: 環(huán)形隊列
* @program: jstl2
* @create: 2019-10-05 09:53
*/
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//創(chuàng)建一個環(huán)形隊列 這里輸入最大數(shù)是4,其實有效的是3
CircleArray queue = new CircleArray(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':
queue.showQueue();
break;
case 'a':
System.out.println("輸出一個數(shù)");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g': // 取出數(shù)據(jù)
try {
int res = queue.getQueue();
System.out.printf("取出的數(shù)據(jù)是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h': // 查看隊列頭的數(shù)據(jù)
try {
int res = queue.headQueue();
System.out.printf("隊列頭的數(shù)據(jù)是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e': // 退出
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
class CircleArray {
private int maxSize; //數(shù)組的最大容量
//front 變量調(diào)整:front就指向隊列的第一個元素,即:arr[front]=arr[0]
private int front;
//rear 變量調(diào)整:rear 初始值為0 指向隊列的最后一個元素的位置,因為需要空出一個位置作約束
private int rear;
private int[] arr;
//構(gòu)造器
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
}
//判斷隊列是否已滿
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
//判斷隊列是否為空
public boolean isEmpty() {
return rear == front;
}
//添加數(shù)據(jù)到隊列
public void addQueue(int n) {
//判斷隊列是否已滿
if (isFull()) {
System.out.println("隊列已滿,不能添加數(shù)據(jù)");
return;
}
//直接加入數(shù)據(jù)
arr[rear] = n;
//rear 后移,后移的時候,要通過取模來實現(xiàn)環(huán)形后移
rear = (rear + 1) % maxSize;
}
//獲取隊列的數(shù)據(jù),出隊列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("隊列為空,不能取數(shù)據(jù)");
}
//由于front是指向隊列的第一個元素,所以需要
// 1、front的值先保存到臨時變量
//2、將front后移一位,通過取模實現(xiàn)環(huán)形后移,
// 3、將臨時變量返回
int temp = arr[front];
front = (front + 1) % maxSize;
return temp;
}
//顯示隊列的所有數(shù)據(jù)
public void showQueue() {
if (isEmpty()) {
System.out.println("隊列為空,沒有數(shù)據(jù)~");
return;
}
//遍歷環(huán)形隊列....這里有個小技巧,需要記住
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
//求出當(dāng)前隊列有效數(shù)據(jù)的個數(shù)
public int size() {
return (rear + maxSize - front) % maxSize;
}
//顯示隊列的頭部數(shù)據(jù)
public int headQueue(){
//判斷是否為空
if (isEmpty()){
throw new RuntimeException("隊列為空,沒有數(shù)據(jù)");
}
return arr[front];
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解
這篇文章主要介紹了 Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個web項目(圖文教程)
這篇文章主要介紹了新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個web項目的圖文教程,需要的朋友可以參考下2018-01-01
IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法
這篇文章主要介紹了IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
解決java main函數(shù)中的args數(shù)組傳值問題
這篇文章主要介紹了解決java main函數(shù)中的args數(shù)組傳值問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
mybatis-plus(insertBatchSomeColumn批量添加方式)
這篇文章主要介紹了mybatis-plus(insertBatchSomeColumn批量添加方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

