【小玩意儿】用数组实现一个小队列~

用数组写一个小队列~

 import java.util.Scanner;/**
  * 使用数组完成队列功能
  */
 public class ArrayQDemo {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.println("输入数字,创建一个队列");
         ArrayQu arrayQu = new ArrayQu(sc.nextInt());while (true){
             System.out.println("*******************");
             System.out.println("1: 展示队列");
             System.out.println("2: 添加队列信息");
             System.out.println("3: 取值");
             System.out.println("9: 退出");
             System.out.println("*******************");
             switch (sc.nextInt()){
                 case 1:{
                     arrayQu.showQ();
                     break;
                 }
                 case 2:{
                     System.out.println("请输入一个数");
                     arrayQu.add(sc.nextInt());
                     break;
                 }
                 case 3:{
                     arrayQu.get();
                     arrayQu.showQ();
                     break;
                 }
                 case 9:{
                     int i = 1;
                     System.exit(i);
                     break;
                 }
                 default:{
                     System.out.println("请输入正确的字符");
                     break;
                 }
             }
         }}
 }
 ​
 ​
 class ArrayQu {
     private int maxSize;
     private int headNode;
     private int bottomNode;
     private int[] arr;public ArrayQu(int arrMaxSize) {
         maxSize = arrMaxSize;
         headNode = 0;
         bottomNode = 0;
         arr = new int[maxSize];
     }public boolean isEmpty() {
         return headNode == bottomNode;
     }public boolean isFull() {
         return headNode == maxSize;
     }public void add(int a) {
         if (!isFull()) {
             arr[headNode] = a;
             headNode++;
             System.out.println("加入成功");
         } else {
             System.out.println("加入失败");
         }
     }public int get() {
         int temp = 0;
         if (isEmpty()) {
             throw new RuntimeException("队列为空,不可取值");
         } else {
            temp = arr[0];
            headNode--;
             for (int i = 0; i < headNode; i++) {
                 arr[i] = arr[i + 1];
             }return temp;}
     }
     public void showQ(){
         if (!isEmpty()){
             for (int i = headNode-1; i >= 0; i--) {
                 System.out.print("|  ");
                 System.out.printf("___"+arr[i]+"___");
                 System.out.println("  |");
             }
         }else {
             System.out.println("队列为空,不可展示");
         }
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值