JAVA 数组模拟环形队列

在上一篇JAVA 数组模拟队列的文章中存在问题:

当队列中的元素全部取出后,输出队列元素为空。再次向队列中添加元素时,提示队列已满,无法添加。

因为队列中数组只能使用一次,没有达到复用的效果。

本文将使用环形队列解决这个问题。

数组模拟环形队列思路分析:

1. 基于之前的 front 变量,在环形队列中, front 调整为 指向队列的第一个元素, 即 arr[front]。front 的初始值 为0。

2. 基于之前的 rear 变量,在环形队列中,rear 调整为指向队列的最后一个元素的前一个位置. 将最后一个位置空出来作为约定。rear 的初始值 为 0。

3. 当队列满时,使用条件  (rear  + 1) % maxSize == front 来判断。(这里rear+1即为最后一个位置的下标,与队列的最大容量取模,再与front相比,front=0)。

4. 当队列为空时,使用条件 rear == front 来判断。

5. 基于上述分析,可以得出队列中有效数据的个数为:   (rear + maxSize - front) % maxSize  。(取模是因为是环形队列,这样可以使队列的首尾相接。如果直接使用rear - front 来获取有效数据个数,可能会出现负数。

代码实现:

1、创建CircleQueue类

1)添加构造方法,完成队列的初始化操作。

 public CircleQueue(int maxSize){
        this.maxSize = maxSize;
        queueArr = new int[maxSize];
    }

2)判断队列是否已满

 //判断队列是否已满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }

3)判断队列是否为空

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

4) 向队列中添加元素

public void addQueue(int num){
        if(isFull()){
            System.out.println("队列已满,不能再添加数据~~~~");
            return;
        }
        queueArr[rear] = num;
        rear = (rear+1)%maxSize;

    }

5)获取队列中有效数据的个数

 public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据~~~~~~");
        }
        int frontVal = queueArr[front];
        front = (front+1)%maxSize;
        return frontVal;
    }

6)获取队列的头部元素

 public int getHeadQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据~~~~~~");
        }
        return queueArr[front];
    }

7) 打印队列中所有有效的数据

 public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空~~~~");
            return;
        }
        for(int i=front;i<front+getQueueNum();i++){
            System.out.printf("QueueArry[%d]=%d\n",i%maxSize,queueArr[i%maxSize]);
        }
    }


 public int getQueueNum(){
        return  (rear+maxSize-front)%maxSize;
    }

8)测试

  public static void main(String[] args) {
        CircleQueue queueArr = new CircleQueue(4);
        Scanner sc = new Scanner(System.in);
        char option =' ';
        boolean flag = true;

        while (flag){
            System.out.println("s(show):查看队列中的所有元素");
            System.out.println("a(add):向队列中添加元素");
            System.out.println("g(get):获取出队列的元素");
            System.out.println("h(head):获取队列的头部元素");
            System.out.println("e(exit):退出队列");

            option= sc.next().charAt(0);

            switch (option){
                case 's':
                    queueArr.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入要插入的元素:");
                    int index = sc.nextInt();
                    queueArr.addQueue(index);
                    break;
                case 'g':
                    try {
                        int  Queue = queueArr.getQueue();
                        System.out.println("取出的元素为:"+Queue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int  head = queueArr.getHeadQueue();
                        System.out.println("队列头部元素为:"+head);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    flag=false;
                    break;
                default:
                    System.out.println("请输入正确的字符");
                    break;
            }


        }
        System.out.println("退出队列~~~~~~~");
    }

完整代码

package com.sc.test02;

import java.util.Scanner;

public class CircleQueueArray {
    public static void main(String[] args) {
        CircleQueue queueArr = new CircleQueue(4);
        Scanner sc = new Scanner(System.in);
        char option =' ';
        boolean flag = true;

        while (flag){
            System.out.println("s(show):查看队列中的所有元素");
            System.out.println("a(add):向队列中添加元素");
            System.out.println("g(get):获取出队列的元素");
            System.out.println("h(head):获取队列的头部元素");
            System.out.println("e(exit):退出队列");

            option= sc.next().charAt(0);

            switch (option){
                case 's':
                    queueArr.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入要插入的元素:");
                    int index = sc.nextInt();
                    queueArr.addQueue(index);
                    break;
                case 'g':
                    try {
                        int  Queue = queueArr.getQueue();
                        System.out.println("取出的元素为:"+Queue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int  head = queueArr.getHeadQueue();
                        System.out.println("队列头部元素为:"+head);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    flag=false;
                    break;
                default:
                    System.out.println("请输入正确的字符");
                    break;
            }


        }
        System.out.println("退出队列~~~~~~~");
    }

}
class CircleQueue{
    int front;
    int rear;
    int maxSize;
    int[]queueArr;

    public CircleQueue(int maxSize){
        this.maxSize = maxSize;
        queueArr = new int[maxSize];
    }

    //判断队列是否已满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }

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

    public void addQueue(int num){
        if(isFull()){
            System.out.println("队列已满,不能再添加数据~~~~");
            return;
        }
        queueArr[rear] = num;
        rear = (rear+1)%maxSize;

    }


    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据~~~~~~");
        }
        int frontVal = queueArr[front];
        front = (front+1)%maxSize;
        return frontVal;
    }

    public int getHeadQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据~~~~~~");
        }
        return queueArr[front];
    }

    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空~~~~");
            return;
        }
        for(int i=front;i<front+getQueueNum();i++){
            System.out.printf("QueueArry[%d]=%d\n",i%maxSize,queueArr[i%maxSize]);
        }
    }

    public int getQueueNum(){
        return  (rear+maxSize-front)%maxSize;
    }
}

输出结果: 

 s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
s
队列为空~~~~
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
a
请输入要插入的元素:
1
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
a
请输入要插入的元素:
2
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
a
请输入要插入的元素:
3
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
g
取出的元素为:1
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
g
取出的元素为:2
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
s
QueueArry[2]=3
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
a
请输入要插入的元素:
4
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
a
请输入要插入的元素:
5
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
s
QueueArry[2]=3
QueueArry[3]=4
QueueArry[0]=5
s(show):查看队列中的所有元素
a(add):向队列中添加元素
g(get):获取出队列的元素
h(head):获取队列的头部元素
e(exit):退出队列
e
退出队列~~~~~~~

 

ok,可以看到,环形队列成功达到复用数据的效果。

 

 

1.算法是程序的灵魂,优秀的程序在对海量数据处理时,依然保持高速计算,就需要高效的数据结构和算法支撑。2.网上数据结构和算法的课程不少,但存在两个问题:1)授课方式单一,大多是照着代码念一遍,数据结构和算法本身就比较难理解,对基础好的学员来说,还好一点,对基础不好的学生来说,基本上就是听天书了2)说是讲数据结构和算法,但大多是挂羊头卖狗肉,算法讲的很少。 本课程针对上述问题,有针对性的进行了升级 3)授课方式采用图解+算法游戏的方式,让课程生动有趣好理解 4)系统全面的讲解了数据结构和算法, 除常用数据结构和算法外,还包括程序员常用10大算法:二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、马踏棋盘算法。可以解决面试遇到的最短路径、最小生成树、最小连通图、动态规划等问题及衍生出的面试题,让你秒杀其他面试小伙伴3.如果你不想永远都是代码工人,就需要花时间来研究下数据结构和算法。教程内容:本教程是使用Java来讲解数据结构和算法,考虑到数据结构和算法较难,授课采用图解加算法游戏的方式。内容包括: 稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。学习目标:通过学习,学员能掌握主流数据结构和算法的实现机制,开阔编程思路,提高优化程序的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值