import java.security.PublicKey;
import java.util.Scanner;
public class queue {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请确定队列大小");
int capacity = input.nextInt();
ArrayQueue queue = new ArrayQueue(capacity);
for(;;){
System.out.println("判断队列是否满,输入 1");
System.out.println("判断队列是否为空,输入 2");
System.out.println("添加元素,输入 3");
System.out.println("显示元素,输入 4");
System.out.println("推出系统,输入 5");
System.out.print("请输入内容:");
String choose = input.next();
switch(choose){
case "1":
queue.isFull();
break;
case "2":
queue.isEmpty();
break;
case "3":
queue.addQueue();
break;
case "4":
queue.showQueue();
break;
case "5":
System.out.println("退出系统!");
return;
default:
System.out.println("输入错误!");
break;
}
}
}
}
class ArrayQueue{
//数组模拟队列
private int mixSize;//队列的最大容量
private int Head;//队列头
private int Tail;//队列尾
private int [] arr;
public ArrayQueue(int mixSize) {
this.mixSize = mixSize;
arr = new int[this.mixSize];
Head = 0;
Tail = 0;
}
//判断队列是否满
public boolean isFull() {
return Tail == mixSize-1;
}
//判断队列是否为空
public boolean isEmpty(){
return Head == Tail;
}
//添加元素
public void addQueue(){
if(isFull()){
System.out.println("该队列已满");
return;
}
Scanner input = new Scanner(System.in);
for (int a = 0;a<=mixSize;a++){
System.out.print("输入存入队列的数据:");
arr[Tail] = input.nextInt();
Tail++;
for (;;){
System.out.println("是否继续存入数据"+"\n"+"若是,则输入1;若不是,则输入任意数字/字母/符号。");
String empty02 = input.next();
if(empty02.equals("1")){
System.out.print("输入存入队列的数据");
arr[Tail] = input.nextInt();
Tail++;
}else {
return;
}
}
}
}
//显示元素
public void showQueue(){
if(isEmpty()){
System.out.println("该队列为空");
return;
}
for (int i= 0;i<=mixSize-1;i++){
System.out.println(arr[i]);
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)