Java大数据平台开发 学习笔记(1)—— 数组模拟环形队列实现

一、数据结构与算法:


Step 1) 定义环形数组元素:

 private int maxSize;//环形数组长度
 private int front;	 //环形数组头指针	
 private int rear;	 //环形数组尾指针
 private int[] arr;	 //环形数组

Step 2) 初始化元素:

  public ArrayQueue(int arrMaxSize){
    
    
      maxSize = arrMaxSize;
      arr = new int[maxSize];
      front = 0;
      rear = 0;
  }

Step 3) 环形数组存满条件:

    public boolean inFull(){
    
    
        return front == (rear + 1) % maxSize;
    }
    

Step 4) 环形数组为空条件:

    public boolean isEmpty(){
    
    
        return rear == front;
    }
    

Step 5) 存入数据方法:

    public void addQueue(int n){
    
    
        if(inFull()){
    
    
            System.out.println("数据已经存满,无法存入");
            return;
        }
        System.out.println("数据存入成功");
        arr[rear++] = n;
        rear%=maxSize;
    }
    

Step 6) 取出数据方法:

    public void getQueu(){
    
    
        if(isEmpty()){
    
    
            System.out.println("数据为空,无法获取");
        }
        System.out.printf("取出的数据为:%d \r\n",arr[front]);
        arr[front++] = 0;
        front%=maxSize;
    }
    

Step 7) 遍历数据方法:

    public void showQueue(){
    
    
        if( isEmpty()){
    
    
            System.out.println("数据为空,无法打印");
            return;
        }
        for (int i=0 ; i<arr.length ; i++) {
    
    
            System.out.printf("arr[%d] = %d \r\n", i, arr[i]);
        }
    }
    

Step 8) 查看数据头方法:

    public void headQueue(){
    
    
        if( isEmpty()){
    
    
            System.out.println("数据为空,没有数据");
        }
        System.out.printf("取出的队列头为:%d \r\n",arr[front]);
    }
    

Step 9) main 方法:

    public static void main(String[] args) {
    
    
        ArrayQueue arrayQueue = new ArrayQueue(4);
        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop){
    
    
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add ): 添加数据");
            System.out.println("g(get ): 取出数据");
            System.out.println("h(head): 查看头的数据");
            key = sc.next().charAt(0);
            switch (key){
    
    
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数据");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
    
    
                        arrayQueue.getQueu();
                    }catch(Exception e) {
    
    
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
    
    
                        arrayQueue.headQueue();
                    }catch(Exception e) {
    
    
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
    

• 由 ChiKong_Tam 写于 2020 年 9 月 4 日

猜你喜欢

转载自blog.csdn.net/qq_42209354/article/details/108413139