zoukankan      html  css  js  c++  java
  • 环形队列

    package com.cxy.springdataredis.data;
    
            import java.util.Scanner;
    
    public class CircleArray {
        public static void main(String[] args) {
            CirCleArrayQueue queue = new CirCleArrayQueue(4);
            boolean loop =true;
            char key ;
            Scanner scanner = new Scanner(System.in);
            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 = scanner.next().charAt(0);// 接收一个字符
                switch (key) {
                    case 's':
                        queue.showQueue();
                        break;
                    case 'a':
                        System.out.println("输出一个数");
                        int value = scanner.nextInt();
                        queue.addQueue(value);
                        break;
                    case 'g': // 取出数据
                        try {
                            int res = queue.getQueue();
                            System.out.printf("取出的数据是%d
    ", res);
                        } catch (Exception e) {
                            // TODO: handle exception
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'h': // 查看队列头的数据
                        try {
                            int res = queue.getHeader();
                            System.out.printf("队列头的数据是%d
    ", res);
                        } catch (Exception e) {
                            // TODO: handle exception
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'e': // 退出
                        scanner.close();
                        loop = false;
                        break;
                    default:
                        break;
                }
            }
            System.out.println("程序退出~~");
    
        }
    }
    class CirCleArrayQueue{
        private int maxSize;
        //表示数组的第一个元素,初始化0
        private int font;
        //表示数组的最后一个元素的最后一个位置
        private int rear;
        private int[] arr;
    
        public CirCleArrayQueue(int arrMaxSize){
            maxSize =arrMaxSize;
            arr =new int[maxSize];
            font =0;
            rear =0;
        }
        public boolean isFull(){
            return (rear+1)%maxSize ==font;
        }
        public boolean isEmpty(){
            return  font ==rear;
        }
        public  void addQueue(int n){
            if (isFull()){
                System.out.println("队列已经满了");
                return ;
            }
            arr[rear] =n;
            //将rear后移必须考虑取模
            rear =(rear+1)%maxSize;
        }
        public int getQueue(){
            if (isEmpty()){
                throw new RuntimeException("队列为空");
            }
            int value =arr[font];
            font =(font+1)%maxSize;
            return value;
        }
        public void showQueue(){
            if (isEmpty()){
                System.out.println("队列为空");
                return ;
            }
            for (int i =font;i<font+getSize();i++){
                System.out.printf("arr[%d]=%d
    ",i%maxSize,arr[i%maxSize]);
            }
        }
        public int getSize(){
            return (rear+maxSize-font)%maxSize;
        }
    
        public int getHeader(){
            if (isEmpty()){
                throw  new RuntimeException("队列为空");
            }
            return arr[font];
        }
    
    
    }
  • 相关阅读:
    Gitlab 与 Git Windows 客户端一起使用的入门流程
    怎样把SEL放进NSArray里
    PerformSelector may cause a leak because its selector is unknown 解决方法
    drawRect
    记录常规越狱的判断方法
    网页 js
    UICollectionView 基础
    FMDB的简单使用
    图层的一些基本动画效果
    NSPredicate简单介绍
  • 原文地址:https://www.cnblogs.com/xiufengchen/p/11540454.html
Copyright © 2011-2022 走看看