队列1

数组模拟简单队列1

class ArrayQueue {
//数组最大长度
private int maxSize;
//队列尾部
private int rearIndex;
//队列头部
private int frontIndex;
//数组队列
private int[] arrayQueue;

/**
 * 构造队列
 * @param maxSize
 */
public void queue(int maxSize){
    this.maxSize=maxSize;
    arrayQueue=new int[this.maxSize];
    rearIndex=-1;
    frontIndex=-1;
}

/**
 * 判断队列是否为空
 * @return
 */
public boolean isEmpty(){
    return rearIndex==frontIndex;
}

/**
 * 判断队列是否为满
 * @return
 */
public boolean isFull(){
    return rearIndex==maxSize-1;
}

/**
 * 添加数据
 * @param num
 */
public void addQueue(int num){
    if(isFull()){
        System.out.println("队列已满,添加失败");
    }else {
        arrayQueue[++rearIndex]=num;
    }
}

/**
 * 取出数据
 * @return
 */
public int getQueue(){
    //采用抛出异常的方式避免int类型的返回
    if(isEmpty()){
        throw new RuntimeException("队列为空");
    }else {
        return arrayQueue[++frontIndex];
    }
}

/**
 * 展示队列
 */
public void showQueue(){
    if(isEmpty()){
        System.out.println("队列为空");
    }else {
        for(int i=frontIndex+1;i<=rearIndex;i++){
            System.out.print(arrayQueue[i]+"\t");
        }
    }
}

/**
 * 展示头部数据,因为采用直接打印的方式,所以无需采用抛异常的方式提示队列空
 * @return
 */
public void headQueue(){
    if (isEmpty()){
        System.out.println("队列为空");
    }else {
        System.out.println(arrayQueue[frontIndex+1]);
    }
}
}

运行

import java.util.Scanner;
public class Queue {
public static void main(String[] args) {
    ArrayQueue queue=new ArrayQueue();
    Scanner input=new Scanner(System.in);

    System.out.println("数组模拟队列初体验");
    queue.queue(5);

    aa:while (true){
        //获取数字指令
        int order=-1;
        while (true){
            System.out.println("请输入操作编号:1-添加\t2-取出\t3-展示\t4-头部\t0-退出");
            String in=input.nextLine();
            try {
                order=Integer.parseInt(in);
                if(order>4 || order<0){
                    System.out.println("指令不在范围内");
                }else {
                    break;
                }
            }catch (RuntimeException e){
                System.out.println("指令不明确");
            }
        }
        //操作
        bb:switch (order){
            case 1:{
                while(true){
                    System.out.println("请输入数字");
                    String in=input.nextLine();
                    try {
                        int a=Integer.parseInt(in);
                        queue.addQueue(a);

                    }catch (RuntimeException e){
                        System.out.println("无法识别");
                    }
                    break bb;
                }
            }
            case 2:{
                try {
                    System.out.println(queue.getQueue());

                }catch (RuntimeException e){
                    System.out.println(e.getMessage());
                }
                break;
            }
            case 3:{
                queue.showQueue();
                break;
            }
            case 4:{
                queue.headQueue();
                break;
            }
            default:{
                break aa;
            }
        }
    }

    System.out.println("体验结束");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值