2-队列

队列


基本介绍

  1. 队列是一个有序列表,可以用数组或是链表来实现。
  2. 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取
  3. 1620459223678

数组模拟队列

不可以复用空间版本

1620459361015

package com.company.queue;

import java.util.Scanner;
import java.util.concurrent.BlockingDeque;

/**
 * @Function :
 * date 2021/5/8 - 15:37
 * How :
 */
public class QueueByArray {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.addQueue(3);
        arrayQueue.addQueue(3);
        arrayQueue.showQueue();
        arrayQueue.getQueue();
        arrayQueue.getQueue();
        arrayQueue.getQueue();
        arrayQueue.showQueue();

    }
}

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;   //指向最后一个元素
    }

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

    public boolean isEmpty(){
        return rear == front;
    }

    public void addQueue(int n){
        if (this.isFull()){
            System.out.println("队列满 不能加入");
            return;
        }
        this.arr[++rear] = n;
    }

    public int getQueue(){
        if (this.isEmpty()){
            throw  new RuntimeException("队列空 不能获取");
        }
        return arr[++front];
    }

    public void showQueue(){
        if (this.isEmpty()){
            throw  new RuntimeException("队列空 不能获取");
        }
        for (int temp : arr){
            System.out.printf("\t"+temp);
        }
        System.out.println();
    }
}

改进

环形队列版本

1620461259381

package com.company.queue;

import javafx.scene.shape.Circle;

import javax.sound.midi.Soundbank;
import java.util.Scanner;
import java.util.concurrent.BlockingDeque;

/**
 * @Function :
 * date 2021/5/8 - 15:37
 * How :
 */
public class CircleQueueByArray {
    public static void main(String[] args) {
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(5);
        System.out.println("是否为空"+circleArrayQueue.isEmpty());
        circleArrayQueue.addQueue(1);
        circleArrayQueue.addQueue(2);
        circleArrayQueue.addQueue(3);
        circleArrayQueue.addQueue(4);
        circleArrayQueue.addQueue(5);
        circleArrayQueue.addQueue(6);
        circleArrayQueue.showQueue();
        int a = circleArrayQueue.getQueue();
        int b = circleArrayQueue.getQueue();
        int c = circleArrayQueue.getQueue();
        System.out.println(a+"--"+b+"--"+c);
        circleArrayQueue.showQueue();
    }
}

class CircleArrayQueue{
    private int maxSize;    //最大值
    private int front;      //头
    private int rear;       //尾
    private int[] arr;      //数据体,模拟队列

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;    //指向队列头部且包含
        rear = 0;     //队尾元素的后一个元素
    }

    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }

    public boolean isEmpty(){
        return rear == front;
    }

    public void addQueue(int n){
        if (this.isFull()){
            System.out.println("队列满 不能加入");
            return;
        }
        this.arr[rear] = n;
        rear = (rear+1)%maxSize;
    }

    public int getQueue(){
        if (this.isEmpty()){
            throw  new RuntimeException("队列空 不能获取");
        }
        int temp = arr[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    public void showQueue(){
        if (this.isEmpty()){
            throw  new RuntimeException("队列空 不能获取");
        }
        for (int i = front; i < front + ((rear-front+maxSize)%maxSize); i++) {
            System.out.println("\t"+arr[i%maxSize]);
        }
        System.out.println();
    }
}

posted @ 2021-11-23 21:06  剪水行舟  阅读(11)  评论(0)    收藏  举报