Flask接口签名sign原理与实例代码浅析
271
2022-09-04
java数组实现循环队列示例介绍
从顶部进去数据,从底部出来数据,用数组实现队列,但是下面这个队列,只能进行一次存数值,取数值,不够完善。
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[]args){
//定义队列大小maxsize
ArrayQueue arrayQueue=new ArrayQueue(3);
Scanner scanner=new Scanner(System.in);
char key=' ';
boolean leap=true;
while(leap){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("h(head):显示头条数据");
System.out.println("a(add):添加数据");
System.out.println("g(get):从程序中取出队列");
System.out.println("请输入一个数");
key= scanner.next().charAt(0);
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'h':
System.out.printf("头数据为%d",arrayQueue.showHead());
break;
case 'a':
System.out.println("输入添加数据");
int data= scanner.nextInt();
arrayQueue.addQueue(data);
break;
case 'g':
arrayQueue.getQueue();
break;
case 'e':
leap=false;
break;
default:
break;
}
}
}
}
class ArrayQueue{
private int maxsize;//队列大小
private int rear;
private int front;
private int [] arry;
public ArrayQueue(int maxsize1){
maxsize=maxsize1;
arry=new int[maxsize];
front =-1;
rear=-1;
}
//判断队列是否满
public boolean isFull(){
return rear==maxsize-1;
}
//增加数据
public void addQueue(int data){
if (isFull()){
System.out.println("队列满了");
}
else{
arry[rear+1]=data;
rear++;
}
}
//显示数据
public void showQueue(){
for (int i = 0; i < arry.length; i++) {
System.out.println();
System.out.printf("%d",arry[i]);
}
}
//是否空
public boolean isEmpety(){
return rear== front;
}
//显示头数据
public int showHead() {
if (isEmpety()) {
throw new RuntimeException("队列是空的");
}
return arry[front +1];
}
public int getQueue(){
if (isEmpety()){
throw new RuntimeException("队列是空");
}
System.out.printf("去除一个队列为%d",arry[++front]);
arry[front]=0;
return 0;
}
}
循环队列,可以进行数据插入和取出,是上个普通队列的增强版。上个队列只能使用一次的情况被解决了
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args){
//定义队列大小maxsize
cirCleArray cirCleArray=new cirCleArray(3);//队列大小是三,但是有效数据是两个
Scanner scanner=new Scanner(System.in);
char key=' ';
boolean leap=true;
while(leap){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("h(head):显示头条数据");
System.out.println("a(add):添加数据");
System.out.println("g(get):从程序中取出队列");
System.out.println("请输入一个数");
key= scanner.next().charAt(0);
switch (key){
case 's':
cirCleArray.showCircleArray();
break;
case 'h':
System.out.printf("头数据为%d",cirCleArray.showHead());
break;
case 'a':
cirCleArray.addQueue();
break;
case 'g':
cirCleArray.getQueue();
JgtxwW break;
http:// case 'e':
leap=false;
break;
default:
break;
}
}
}
}
class cirCleArray{
private int maxsize;
private int front;
private int rear;
private int [] arry;
Scanner scanner=new Scanner(System.in);
public cirCleArray(int maxsize1){
this.maxsize=maxsize1;
arry=new int[maxsize];
front=0;
rear=0;
}
//显示队列
public void showCircleArray(){
if (isEmpty()){
System.out.println("队列为空");
}
else{
for (int i =front; i < front+size(); i++) {
System.out.printf("arry[%d]=%d\n",i % maxsize,arry[i % maxsize]);
}
}
}
public int size(){
return (rear+maxsize-front)%maxsize;
}
//队列为空
phttp://ublic boolean isEmpty(){
return front==rear;
}
//队列为满
public boolean isFull(){
return (rear+1)%maxsize==front;
}
//添加数据
public void addQueue(){
if (isFull()){
System.out.printf("队列满了");
}
else{
System.out.printf("输入添加的数据");
int data=scanner.nextInt();
arry[rear]=data;
if (rear rear++; } else { rear=(rear+1)%maxsize; } } } //取出数据 public void getQueue(){ if (isEmpty()){ System.out.println("队列是空的"); } else{ System.out.printf("取出数据%d",arry[front]); // arry[front]=0; // front++; front=(front+1)%maxsize; } } //显示头数据 public int showHead() { if (isEmpty()) { throw new RuntimeException("队列是空的"); } return arry[front]; } }
rear++;
}
else {
rear=(rear+1)%maxsize;
}
}
}
//取出数据
public void getQueue(){
if (isEmpty()){
System.out.println("队列是空的");
}
else{
System.out.printf("取出数据%d",arry[front]);
// arry[front]=0;
// front++;
front=(front+1)%maxsize;
}
}
//显示头数据
public int showHead() {
if (isEmpty()) {
throw new RuntimeException("队列是空的");
}
return arry[front];
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~