数据结构学习三、队列

线性结构-队列

使用场景:银行排队

队列介绍

  • 队列是一个有序列表,可以使用数组或者链表来实现
  • 遵循先入先出原则

队列的实现方式

数组模拟队列
  • 队列本身是有序列表,若用数组实现,则假设最大值为maxSize
  • 分别设置 rear 与 front 记录队列的 尾部与头部
  • front(头)随着数据输出而变化
  • rear(尾)随着数据输入而变化
入队(addQueue)
  • 将尾指针(rear)后移一位,rear+1,即 当 front == rear时,队列为空
  • 若尾指针(rear)小于队列的最大下标 maxSize - 1,则将数据存入到rear所指的数组元素中,否则无法存入数据。 即 当 rear = maxSize - 1时,队列为满

代码实现

package cn.imut.array;

public 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;     //指向队列头部(不包含),前一个位置
        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("队列满,不能加入");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    //数据出队列
    public int getQueue(){
        //判断队列是否空
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        front++;        //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\n", i, arr[i]);
        }
    }

    //显示队列的头数据是多少
    public int headQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front + 1];
    }
}
测试
package cn.imut.array;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import sun.misc.Cache;

import java.util.Scanner;

public class ArrayQueueTest {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';     //接收用户输入

        Scanner sc = 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 = sc.next().charAt(0);      //接收一个字符

            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入数:");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出!");
    }
}
优化
  • 目前数组只能使用一次,不能复用
  • 应当改成环形队列
数组模拟环形队列(取模)
  • front指向队列第一个元素(元素本身)(出队,front+1
  • rear指向队列最后一个元素的后一个位置,因为空出一个空间作为约定(入队,rear=(rear + 1)% maxSize
  • 当队列满时,条件是 (rear + 1) % maxSize = front[满]
  • 当队列为空时,rear == front
  • front初始值为0 rear也为0
  • 队列中的有效个数为(rear + maxSize - front) % maxSize
  • 最大容量为maxSize - 1

代码实现

package cn.imut.array;

//环形队列
public class CircleArrayQueue {
    private int maxSize;        //表示数组最大容量
    private int front;          //执向队列头
    private int rear;           //指向队列尾

    private int[] arr;          //模拟队列

    //创建队列构造器
    public CircleArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

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

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

    //添加数据
    public void addQueue(int n) {
        //判断队列是否满
        if(isFull()){
            System.out.println("队列满,不能加入");
            return;
        }
        arr[rear] = n;
        //将rear后移,必须考虑取模
        rear = (rear + 1) % maxSize;
    }

    //数据出队列
    public int getQueue(){
        //判断队列是否空
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        //需要先分析出front是指向队列的第一个元素
        //1.先把front对应的值保留到一个临时变量
        //2.将front后移(考虑取模)
        //3.将临时保存的变量返回
        int value = arr[front];
        front = (front + 1) % maxSize;                //front后移
        return value;
    }

    //显示队列所有数据
    public void showQueue() {
        //遍历
        if(isEmpty()){
            System.out.println("队列空");
            return;
        }
        //思路:从front开始遍历
        for(int i = front; i < front + size() % maxSize; i++) {
            System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //求当前队列有效数据的个数
    public int size() {
        //rear = 2 front = 1 maxSize = 3
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列的头数据是多少
    public int headQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}
测试
package cn.imut.array;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import sun.misc.Cache;

import java.util.Scanner;

public class CircleArrayQueueTest {
    public static void main(String[] args) {
        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);  //最大容量为3

        char key = ' ';     //接收用户输入

        Scanner sc = 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 = sc.next().charAt(0);      //接收一个字符

            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入数:");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出!");
    }
}

猜你喜欢

转载自www.cnblogs.com/yfyyy/p/12444843.html