循环队列的java结构_java数据结构之循环队列(数组实现)
package com.ws.隊(duì)列.數(shù)組環(huán)形隊(duì)列;
//環(huán)形數(shù)組隊(duì)列
//判斷滿:尾+1%隊(duì)列長(zhǎng)度==頭
//添加數(shù)據(jù):要(尾+1)%數(shù)組長(zhǎng)度
//取出數(shù)據(jù):要(頭+1)%數(shù)組長(zhǎng)度 因?yàn)檫@兩個(gè)都是循環(huán)的,相當(dāng)于一個(gè)圓環(huán),%數(shù)組長(zhǎng)度就是轉(zhuǎn)圈
//隊(duì)列有效數(shù)據(jù)個(gè)數(shù):(尾+數(shù)組長(zhǎng)度-頭)%數(shù)組長(zhǎng)度 數(shù)組因?yàn)槭莻€(gè)圈,所以可能出現(xiàn)頭>尾的情況,所以要提前轉(zhuǎn)一圈,保證尾>頭
//取數(shù)據(jù):i%數(shù)組長(zhǎng)度
//因?yàn)榈阶詈笠粋€(gè)時(shí)判斷空是尾+1然后取余,實(shí)際數(shù)組最后一個(gè)空間存不上,所以實(shí)際的有效隊(duì)列長(zhǎng)度是maxSize-1
import java.util.Scanner;
public class ArrayQueue {
public static void main(String[] args) {
//測(cè)試
Array array=new Array(3);
char key=' ';//接收用戶輸入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while (loop){
System.out.println("a:顯示隊(duì)列");
System.out.println("b:退出程序");
System.out.println("c:添加數(shù)據(jù)到隊(duì)列");
System.out.println("d:從隊(duì)列取出數(shù)據(jù)");
System.out.println("e:顯示隊(duì)列頭數(shù)據(jù)");
key=scanner.next().charAt(0);//接收一個(gè)字符
switch (key){
case 'a':
array.printqueue();
break;
case 'c':
System.out.println("輸入一個(gè)數(shù)");
int value=scanner.nextInt();
array.addArray(value);
break;
case 'd':
try {
int get=array.getArray();
System.out.println("取出的數(shù)據(jù)是:"+get);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
try {
System.out.println("隊(duì)列頭數(shù)據(jù)是:"+array.printtou());
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'b':
scanner.close();
loop=false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
//使用數(shù)組模擬一個(gè)隊(duì)列
class Array{
private int maxSize;//數(shù)組最大容量
private int tou;//隊(duì)列頭
private int wei;//隊(duì)列尾
private int arr[];//數(shù)組,存數(shù)據(jù)
//創(chuàng)建隊(duì)列構(gòu)造器
public Array(int maxSize){
this.maxSize=maxSize;
arr=new int[maxSize];
tou=0;//隊(duì)列頭數(shù)據(jù)
wei=0;//隊(duì)列尾數(shù)據(jù)
}
//判斷隊(duì)列是否滿
public boolean ifMax(){
return (wei+1)%maxSize==tou;
}
//判斷隊(duì)列是否為空
public boolean ifFull(){
return tou==wei;
}
//添加數(shù)據(jù)到隊(duì)列
public void addArray(int queue){
//判斷隊(duì)列是否滿
if (ifMax()){
System.out.println("隊(duì)列滿不能添加數(shù)據(jù)");
return;
}
//直接將數(shù)據(jù)加入
arr[wei]=queue;
//尾后移,得考慮取模
wei=(wei+1)%maxSize;
}
//出隊(duì)列
public int getArray(){
//判斷隊(duì)列是否為空
if (ifFull()){
//拋出異常
throw new RuntimeException("隊(duì)列為空!不能取數(shù)據(jù)");
}
//指向隊(duì)列第一個(gè)元素
int value=arr[tou];
tou=(tou+1)%maxSize;
return value;
}
//顯示隊(duì)列的所有數(shù)據(jù)
public void printqueue(){
if (ifFull()){
System.out.println("隊(duì)列為空,沒(méi)有數(shù)據(jù)");
return;
}
//從頭開(kāi)始遍歷,遍歷有效數(shù)據(jù)個(gè)數(shù)
for (int i=tou;i
System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
}
}
//求出當(dāng)前隊(duì)列有效數(shù)據(jù)個(gè)數(shù)
public int size(){
return (wei+maxSize-tou)%maxSize;//就是轉(zhuǎn)圈
}
//顯示隊(duì)列的頭是
public int printtou(){
//判斷隊(duì)列空
if (ifFull()){
throw new RuntimeException("隊(duì)列空,無(wú)頭數(shù)據(jù)");
}
return arr[tou];
}
}
標(biāo)簽:java,隊(duì)列,System,int,maxSize,println,數(shù)據(jù)結(jié)構(gòu),out
來(lái)源: https://blog.csdn.net/wangshuo2020/article/details/112403087
總結(jié)
以上是生活随笔為你收集整理的循环队列的java结构_java数据结构之循环队列(数组实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2011款蒙迪欧致胜雨刮器坏了,刮着刮就
- 下一篇: 克隆需要验证_[实验技巧]CRISPR实