通过数组模拟简单队列和环形队列

废话不多说,直接上代码:

        简单队列

import java.util.Scanner;

public class Test01 {
    private Integer maxSize;
    private Integer front;
    private Integer rear;
    private Integer[] arr;

    public Test01(Integer maxSize) {
        this.maxSize = maxSize;
        this.front = -1;
        this.rear = -1;
        this.arr = new Integer[maxSize];
    }
    public boolean isNull(){
        return front.equals(rear);
    }
    public boolean isFull(){
        return rear.equals(maxSize-1);
    }
    public void addQueue(int n){
        if (isFull()){
            throw new RuntimeException("队列已经满");

        }
        rear+=1;
        arr[rear] = n;
    }
    public int getQueue(){
        if (isNull()){
            throw new RuntimeException("队列为空,无法取出");
        }
        front+=1;
        return arr[front];
    }
    public void show(){
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front+1; i < rear+1; i++) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
    public int headHead(){
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front+1];
    }
}
class Main{
    public static void main(String[] args) {
        Test01 test= new Test01(3);
        char key =' ';
        Scanner s =new Scanner(System.in);
        boolean loop =true;
        while (loop){
            System.out.println("1:显示队列");
            System.out.println("2:退出程序");
            System.out.println("3: 添加队列");
            System.out.println("4: 拿出队列");
            System.out.println("4: 查看头队列");
            char next = s.next().charAt(0);
            switch (next) {
                case '1':
                    try {
                        test.show();

                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case '2':
                    loop =false;
                    break;
                case '3':
                    System.out.println("输出一个数");
                    int i = s.nextInt();
                    try {
                        test.addQueue(i);
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case '4':

                    try {
                        int queue = test.getQueue();
                        System.out.println("你取出的数据是:"+queue);
                    }catch(Exception e){
                        System.out.println(e.getMessage());                    }

                    break;
                case '5':
                    try {

                        test.headHead();
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }

    }
}

        环形队列:

import java.util.Scanner;

public class Test02 {
    private Integer maxSize;
    private Integer front;
    private Integer rear;
    private Integer[] arr;

    public Test02(Integer maxSize) {
        this.maxSize = maxSize;
        this.front = 0;
        this.rear = 0;
        this.arr = new Integer[maxSize];
    }

    public boolean isNull() {
        return front.equals(rear);
    }


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

    }

    public void addQueue(int n) {
        if (isFull()) {
            throw new RuntimeException("队列已经满");
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isNull()) {
            throw new RuntimeException("队列为空,无法取出");
        }
        int value = arr[front];
        front += 1;
        return value;
    }

    public void show() {
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front+((rear + maxSize - front) % maxSize); i++) {
            System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
        }
    }

    public int headHead() {
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

class Main02 {
    public static void main(String[] args) {
        Test02 test = new Test02(4);
        char key = ' ';
        Scanner s = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("1:显示队列");
            System.out.println("2:退出程序");
            System.out.println("3: 添加队列");
            System.out.println("4: 拿出队列");
            System.out.println("5: 查看头队列");
            char next = s.next().charAt(0);
            switch (next) {
                case '1':
                    try {
                        test.show();

                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case '2':
                    loop = false;
                    break;
                case '3':
                    System.out.println("输出一个数");
                    int i = s.nextInt();
                    try {
                        test.addQueue(i);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case '4':

                    try {
                        int queue = test.getQueue();
                        System.out.println("你取出的数据是:" + queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case '5':
                    try {

                        int i1 = test.headHead();
                        System.out.println("头队列:"+i1);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }


    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值