2020-06-08数据结构与算法-数组模拟循环列表

2020-06-08数据结构与算法-数组模拟循环列表

import java.util.Scanner;

public class CircleArrayQueue {
    public static void main(String[] args) {
          //测试
        System.out.println("...............数组模拟环形队列................");
        //创建一个队列
        CircleArray  arrayqueue=new CircleArray(4);
        char k=' ';  //接收用户输入
        //扫描器
        Scanner scanner =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),取出队列头");
            k=scanner.next().charAt(0);  //接收一个字符
            switch(k){
                case 's':
                    arrayqueue.showQueue();
                    break;
                case 'a':
                    System.out.println("........输入一个数.......");
                    int value=scanner.nextInt();
                    arrayqueue.AddQueue(value);
                    break;
                case 'g':
                    try{
                        int res=arrayqueue.getQueue();
                        System.out.printf("取出的数据是%d\n",res);
                        break;
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': //查看队列头的数据
                    try{
                        int rs=arrayqueue.showFront();
                        System.out.printf("队列的头的数据是%d\n",rs);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
                default:
                    break;
            }

        }
        System.out.println(".............程序退出................");

    }
}
    class CircleArray{
        private int maxSize;  //队列的最大容量
        private int front;  //队列头 调整初始值 front=0;
        private int rear;    //队列尾  rear=0;
        private int[] arr; //模拟数组
        //构造队列的构造器
        public  CircleArray(int ArrMaxSize){
            maxSize=ArrMaxSize;
            arr=new int[maxSize];
            front=0;  //指向队列头的前一个位置
            rear=0;   //指向队列尾的最后一个数的位置
        }
        //判断队列是否是满的
        public boolean isFull(){
            return (rear+1)%maxSize==front;
        }
        //判断队列是否为空
        public boolean isEmpty(){
            return rear==front;
        }
        //添加数据到队列
        public void AddQueue(int n){
            //判断队列是否已满
            if(isFull()){
                System.out.println("队列已满,不能添加数据");
                return;
            }else{
                //直接将数据加入
                arr[rear]=n; //添加数据
                //将rear后移,考虑rear取模的问题
                rear=(rear+1)%maxSize;


            }
        }
        //数据出队列
        public int getQueue(){
            if(isEmpty()){
                throw new RuntimeException("队列空,不能取数据");
            }else{
                //这里需要指出队列front指向第一个元素
                int value=arr[front];
                front=(front+1)%maxSize;
                return value;
                //1.取出队列的元素保存到一个临时变量
                //2.将front位置后移
                //返回临时变量

            }
        }
        //遍历队列所有数据
        public void showQueue(){
            if(isEmpty()){
                System.out.println("队列为空,没有数据");
                return;
            }else{
                for(int i=front;i<front+size();i++){
                    System.out.printf("arr[%d\n]=",i%maxSize,arr[i]);
                }

                }
            }
        public int size(){
            return (rear+maxSize-front) % maxSize;
        }
    //显示队列的头数据
    public int showFront(){
        if(isEmpty()){
            throw new RuntimeException("队列空,没有头数据");
        }else{
            return arr[front];
        }
    }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值