JAVA版本的队列

队列

1、数组模拟队列

(1)队列本身是序列表,若使用数组来储存队列的数据,需要声明MaxSize,即该队列最大容量

(2)队列的输入输出从前后端来处理,用front和reae分别来记录队列的前后端,当插入数据时,front不变,rear变;当输出数据时,rear不变,front变

(3)当指针font = rear时表示队列为空,当rear = maxSize-1时,表示满了

在这里插入图片描述

代码如下:

package queue;

import java.util.Queue;
import java.util.Scanner;

/**
 * 用数组模拟队列
 */
public class ArraryQueueDemo {
    public static void main(String[] args) {
        // 创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' '; // 接受用户输入
        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):查看队列头数据");
            key = scanner.next().charAt(0); //接受一个字符串
            switch (key) {
                case 's' :
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个整数");
                    int value = scanner.nextInt();
                    try{
                    	arrayQueue.addQueue(value);
                    }catch(Exception e){
						System.out.println(e.getMessage());
					}
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.println("获取的数据为: " + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.println("获取的数据为: " + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }

}

class ArrayQueue {
    private int maxSize; // 表示队列的最大容量
    private int front; // 队首
    private int rear; //队尾
    private int[] arr; //该数据用于存放数据,模拟队列

    //创建队列的构造器
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1; //指向队列头部
        rear = -1; // 指向队列尾部,开始没有数据所有都是-1
    }

    public boolean  isFull() {
        return rear == maxSize -1 ;
    }
    public boolean isEmpty() {
        return front == rear;

    }

    // 进队列
    public void addQueue(int n) {
        // 判断队列是否满了
        if(isFull()) {
            System.out.println("队列满了");
            return;
        }
        rear++; // 让rear后移
        arr[rear] = n;

    }

    // 取出数据,出队列

    public int getQueue(){
        // 判断队列是否空
        if(isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }
        front++;
        return arr[front];

    }

    //显示队列的所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空,无数据");
            return;
        }
        for (int i = 0; i < arr.length ; i++){
            System.out.printf("arr[%d] = %d \t",i,arr[i]);
        }
    }

    public int headQueue(){
        if(isEmpty()){
            throw new  RuntimeException("队列为空");
        }
        return arr[front + 1];
    }




}


存在的问题,该队列是一次性的,当队列满了就无法添加新数据,因此模拟成环形

模拟环形队列

(1)修改front和rear的含义,front指向第一个元素,rear指向最后一个元素的后一个位置,留出一个位置方便判断。front和rear的初始值均是0。

(2)当队列满的时候,条件是(rear + 1)% maxSize = front,(因为数组是从0开始)

(3)当队列为空时,条件为rear = front

在这里插入图片描述
在这里插入图片描述

上面的图是不全面的,因为这样依旧会浪费空间,并且需要一个很大的数组,因为front和rear一直在增加

进一步改进:

在这里插入图片描述
引用一下别人的图

根据这个模型可以得出新的结构

(1)front和rear的起始值均为0

(2)当入队列时,rear = (rear+1)%maxSize

出队列时,front = (front + 1)%maxSize

(3)当队列为空时,front = rear

当队列满时,(rear + 1)% maxSize = front

(4)有效数据的个数为(rear + maxSize - front)-1

因为rear再这种情况下不一定比front大

总结:

原理是利用了取模的周期性,使得数组出现周期变化的情况,并不是使用了一个环形的数据结构

代码如下

package queue;

import java.util.Scanner;

/**
 * 用数组模拟环形队列
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
        // 创建一个队列
        CircleQueue arrayQueue = new CircleQueue(5);
        char key = ' '; // 接受用户输入
        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):查看队列头数据");
            key = scanner.next().charAt(0); //接受一个字符串
            switch (key) {
                case 's' :
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个整数");
                    int value = scanner.nextInt();
                    try {
                        arrayQueue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.println("获取的数据为: " + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.println("获取的数据为: " + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}
class CircleQueue {
    private int front;
    private int rear;
    private int maxSize;
    private int[] arr;

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }
    public boolean isFull(){
        return (rear + 1) % maxSize == front;
    }
    public boolean isEmpty(){
        return rear == front;
    }
    public void addQueue(int data){
        if(isFull()) {
            throw new RuntimeException("队列已经满了,无法入队列");
        }
        arr[rear] = data;
        rear = (rear + 1) % maxSize;
    }
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int getdata = arr[front];
        front = (front + 1) % maxSize;
        return getdata;
    }
    public int headQueue(){
        return arr[front];
    }
    //显示队列的所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空,无数据");
            return;
        }
        for (int i = 0; i < arr.length ; i++){
            System.out.printf("arr[%d] = %d \t",i,arr[i]);
        }
        System.out.printf("此时队首为%d,队尾为%d",front,rear);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值