数据结构之队列(数组实现)

(一)队列介绍

队列是一个有序列表,可以用数组或者是链表实现,队列遵循先进先出的原则,即先存入队列的数据,先取出,后存入队列的数据,后取出.
在这里插入图片描述

(二)数组模拟队列的思路

结合上图,因为队列的输出和输入是分别从前后端来处理,因此需要两个变量front和rear分别记录队列前后端的下标,front会随着数据输出二改变,而rear是随着数据的输入而改变.
分析:
(1)将尾指针往后移,rear+1,当front ==rear时,队列空.
(2)假如尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据,当rear == maxSize-1时,队列满

基本代码实现

package com.dataStructure.com.dataStructure.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        arrayQueue queue = new arrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出队列");
            System.out.println("a:添加队列");
            System.out.println("g:从队列取数据");
            System.out.println("h:获取队列头数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("please input data");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = queue.getQueue();
                        System.out.println("get resulr is "+result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int headQueue = queue.headQueue();
                        System.out.println("head data id"+headQueue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("program was quit");
    }
}
class arrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;
    //创建队列的构造器
    public arrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;  //指向队列头部,分析出front是指向队列头的前一个位置
        rear = -1;  //指向队列的尾部(就是队列的最后一个数据)
    }
    //判断队列是否已满
    public boolean isFull(){
        return rear == maxSize-1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }
    //添加数据到队列
    public void addQueue(int n){
        if(isFull()){
            System.out.println("queue is full,can not add data");
            return ;
        }
        rear ++;
        arr[rear] = n;
    }
    //获取队列的数据
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("queue is empty,can not get data");
        }
        front ++;
        return arr[front];
    }
    //显示队列的所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("queue is empty,can not show data");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println("arr"+arr[i]);
        }
    }
    //显示队列的头数据
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("queue is empty,can not show data");
        }
        return arr[front+1];
    }
}

以上代码实现了队列的基本功能,但是存在一下优化:

目前数据使用一次就不能使用,没有达到复用的效果

接下来我们尝试实现一下: 为了充分利用数组,因此将数组看做是一个环形的(通过取模的方式来实现),思路如下:
(1)front变量的含义做一个调整,front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0.
(2)rear变量的含义做一个调整,rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear的初始值为0.
(3)当队列满时,条件是(rear+1)%maxSizefront.
(4)队列为空的条件,rear
front.
(5)当我们这样分析,队列中有效的数据的个数(rear+maxSize-front)%maxSize.

package com.dataStructure.com.dataStructure.queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArray circleArray = new CircleArray(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出队列");
            System.out.println("a:添加队列");
            System.out.println("g:从队列取数据");
            System.out.println("h:获取队列头数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    circleArray.showQueue();
                    break;
                case 'a':
                    System.out.println("please input data");
                    int value = scanner.nextInt();
                    circleArray.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = circleArray.getQueue();
                        System.out.println("get resulr is "+result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int headQueue = circleArray.headQueue();
                        System.out.println("head data id"+headQueue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("program was quit");
    }
}
class CircleArray{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public CircleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }
    //判断队列是否满
    public boolean isFull(){
        return (rear+1) % maxSize == front;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return front == rear;
    }
    //队列添加数据
    public void addQueue(int n){
        if(isFull()){
            System.out.println("queue is full");
            return;
        }
        arr[rear] = n;
        rear = (rear+1) % maxSize;
    }
    //取出队列数据
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("queue is empty");
        }
        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }
    //获取队列有效元素的个数
    public int size(){
        return (rear +maxSize - front) % maxSize;
    }
    //显示所有数据
    public void showQueue(){
        if(isEmpty()){
            System.out.println("queue is empty");
            return;
        }
        for (int i = front; i <front +size() ; i++) {
            System.out.println(arr[i]);
        }
    }
    //显示第一个数据
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("queue is empty");
        }
        return arr[front];
    }
}

注意,以上代码front的定义和rear的第一与基础版有区别,在分析代码的时候,一定先弄清变量的含义,再分析代码.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值