zoukankan      html  css  js  c++  java
  • 用数组实现 最简 非环形队列

    可以比较直观地理解队列的模型~~


     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("队列为空,不可展示");
            }
        }
     }

     

  • 相关阅读:
    作为【开发人员】如何持续提升自己的开发技能
    永远不要放弃做梦的权利---与所有程序员们共勉
    十种更好的表达“你的代码写的很烂”的方法---总有些人的代码让人难以忍受
    程序员技术练级攻略--练成这样,成神仙了!
    创业其实是个逻辑问题![想不想创业都来看看]
    多图震撼!数字的未来,2013报告
    记最难忘的一件事 等笑话一箩筐
    HDU4666 Hyperspace(曼哈顿)
    POJ3436 ACM Computer Factory(最大流)
    再思考
  • 原文地址:https://www.cnblogs.com/lms2020/p/12370736.html
Copyright © 2011-2022 走看看