队列Queue

队列 数组实现

package one.queue;

import java.util.Scanner;

/**
 * @author : 赵兴宇
 * 队列 数组实现
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayQueue queue = new ArrayQueue(3);
        while (true) {
            System.out.print("s(showQueue):显示队列数据\t");
            System.out.print("a(addQueue):添加元素\t");
            System.out.print("m(isFull):队列满了?\t");
            System.out.print("k(isEmpty):队列空的?\t");
            System.out.print("h(headQueue):队列头数据 \t");
            System.out.println("g(getQueue):数据出队列");
            String key = scanner.next();
            switch (key) {
                case "s":
                    queue.showQueue();
                    break;
                case "a":
                    int i = scanner.nextInt();
                    queue.addQueue(i);
                    break;
                case "m":
                    System.out.println(queue.isFull());
                    break;
                case "k":
                    System.out.println(queue.isEmpty());
                    break;
                case "h":
                    System.out.println(queue.headQueue());
                    break;
                case "g":
                    try {
                        System.out.println(queue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("????????????");
                    scanner.close();
                    return;
            }
        }


    }

}

class ArrayQueue {
    private int maxSize; // 表示队列最大容量
    private int front; // 队列头
    private int rear; // 队列尾部
    private int[] arr; //存放数据 模拟队列

    public ArrayQueue(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.front = -1;
        this.rear = -1;
        this.arr = new int[arrMaxSize];
    }

    // 队列满了?
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    // 队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 添加元素进入队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满了,不能加入" + n);
            return;
        }
        rear++;
        arr[rear] = n;
    }

    // 数据出队列
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空。。。");
        }
        front++;
        return arr[front];
    }

    // 显示队列数据
    public void showQueue() {
        // 显示未出队列的数据
        for (int i = front + 1; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // 显示队列头的数据
    public int headQueue() {
        return arr[front + 1];
    }

}

队列 环形数组实现

package one.queue;

import java.util.Scanner;

/**
 * @author 赵兴宇
 * 环形数组 实现队列
 */
public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CircleArrayQueue queue = new CircleArrayQueue(4); // 有效大小3个
        while (true) {
            System.out.print("s(showQueue):显示队列数据\t");
            System.out.print("a(addQueue):添加元素\t");
            System.out.print("m(isFull):队列满了?\t");
            System.out.print("k(isEmpty):队列空的?\t");
            System.out.print("h(headQueue):队列头数据 \t");
            System.out.println("g(getQueue):数据出队列");
            String key = scanner.next();
            switch (key) {
                case "s":
                    queue.showQueue();
                    break;
                case "a":
                    int i = scanner.nextInt();
                    queue.addQueue(i);
                    break;
                case "m":
                    System.out.println(queue.isFull());
                    break;
                case "k":
                    System.out.println(queue.isEmpty());
                    break;
                case "h":
                    System.out.println(queue.headQueue());
                    break;
                case "g":
                    try {
                        System.out.println(queue.getQueue());
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("????????????");
                    scanner.close();
                    return;
            }
        }


    }

}

class CircleArrayQueue {

    private int maxSize; // 表示队列最大容量
    private int front; // 队列头 0
    private int rear; // 队列尾部 0
    private int[] arr; //存放数据 模拟队列

    public CircleArrayQueue(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.arr = new int[arrMaxSize];
    }

    // 队列满了?
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    // 队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 添加元素进入队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满了,不能加入" + n);
            return;
        }
        arr[rear] = n;
        // rear 取模 元素后移 
        rear = (rear + 1) % maxSize;
    }

    // 数据出队列
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空。。。");
        }
        // 将数据保存起来 在移动下标 要不然就没有机会移动
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    // 显示队列数据
    public void showQueue() {
        // 显示未出队列的数据
        for (int i = front; i < front + (rear + maxSize - front) % maxSize; i++) {
            System.out.println("arr[" + i % maxSize + "] = " + arr[i % maxSize]);
        }
        System.out.println();
    }

    // 显示队列头的数据
    public int headQueue() {
        return arr[front];
    }


}
  • 取模计算机术语,在数学中就是取余
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明明吃了饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值