数据结构--队列

队列介绍


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

实现队列

数组模拟队列

package edu.cqupt._02队列;

/**
 * @description  数组模拟队列
 * @author  lin.z
 * @Date 2020/08/28
 */
public class ArrayQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

    /**
     * @description 判断队列是否满
     * @return boolean
     */
    public boolean isFull(){
        return rear == maxSize - 1;
    }

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

    /**
     * @description  入队列
     * @param element
     */
    public void push(int element){
        if(isFull()){
            System.out.println("队列满,不能插入数据。");
            return;
        }else{
            arr[++rear] = element;
        }
    }

    /**
     * @description  出队列
     * @return
     */
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能取出数据。");
        }else{
            return arr[++front];
        }
    }

    /**
     * @description  显示队列头元素(此处不是取出,front不能变化)
     * @return
     */
    public int head(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,没有头元素。");
        }else{
            return arr[front+1];
        }
    }

    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,没有头元素。");
        }else{
            for (int i = 0; i <arr.length ; i++) {
                System.out.printf("arr[%d] = %d	", i,arr[i]);
            }
            System.out.println();
        }
    }
}

测试类

package edu.cqupt._02队列;

import java.util.Scanner;

public class DemoApp {

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        System.out.println("s(show),显示队列");
        System.out.println("e(exit),退出程序");
        System.out.println("a(push),添加元素");
        System.out.println("g(pop),取出元素");
        System.out.println("h(head),显示队列头元素");
        while (flag){
            System.out.println("-------------");
            key = scanner.next().charAt(0); // 接收一个字符
            switch (key){
                case 'a':
                    System.out.println("请输入一个数:");
                    int element = scanner.nextInt();
                    queue.push(element);
                    break;
                case 'g':
                    int res = queue.pop();
                    System.out.println("取出的数据为:" + res);
                    break;
                case 'h':
                    int h = queue.head();
                    System.out.println("队列头元素为:" + h);
                    break;
                case 's':
                    System.out.println("队列中的数据为:");
                    queue.show();
                    break;
                case 'e':
                   scanner.close();
                   flag = false;
                   break;
                default:
                    break;
            }
        }
    }
}
  • 测试结果

数组模拟环形队列

对前面的数组模拟队列的优化,充分利用数组.因此将数组看做是一个环形的。(通过取模的方式来实现即可)

  • 队列为空的情况
    队列为空:front == rear;
  • 入队列及队列长度
    入队列:arr[rear] = element; rear =(rear+1)% maxSize;
    队列长度:(rear + maxSize - front) % maxSize;
  • 出队列以及队列为满的情况
    出队列:int value = arr[front]; front = (front+1) % maxSize; 返回value,value即为取出的元素
    队列为满:(rear + 1) % maxSize == front
package edu.cqupt._02队列;
/**
 * @description  数组模拟循环队列
 * @author  lin.z
 * @Date 2020/08/31
 */
public class CircleQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;


    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        front = 0;
        rear = 0;
        arr = new int[maxSize];
    }

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

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

    /**
     * @description:入队列
     * @param:element
     */
    public void push(int element){
        if(isFull()){
            System.out.println("队列满,不能插入数据。");
            return;
        }else{
            arr[rear] = element;
            rear =  (rear+1)% maxSize;
        }
    }

    /**
     * @description:出队列
     * @return:int
     */
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能取出数据。");
        }else{
            int value = arr[front];
            front = (front + 1) % maxSize;
            return value;
        }
    }

    /**
     * @description:显示队列头元素(此处不是取出,front不能变化)
     * @return:int
     */
    public int head(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,没有头元素。");
        }else{
            return arr[front];
        }
    }

    /**
     * @description:队列中的元素个数
     * @return:int
     */
    public int length(){
         return (rear + maxSize - front) %  maxSize;

    }

    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,没有头元素。");
        }else{
            for (int i = 0; i <arr.length ; i++) {
                System.out.printf("arr[%d] = %d	", i%maxSize,arr[i%maxSize]);
            }
            System.out.println();
        }
    }
}

测试类

package edu.cqupt._02队列;

import java.util.Scanner;

public class DemoApp2 {
    public static void main(String[] args) {
        CircleQueue queue = new CircleQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        System.out.println("s(show),显示队列");
        System.out.println("e(exit),退出程序");
        System.out.println("a(push),添加元素");
        System.out.println("g(pop),取出元素");
        System.out.println("l(pop),队列求长度");
        System.out.println("h(head),显示队列头元素");
        while (flag){
            System.out.println("-------------");
            key = scanner.next().charAt(0); // 接收一个字符
            switch (key){
                case 'a':
                    System.out.println("请输入一个数:");
                    int element = scanner.nextInt();
                    queue.push(element);
                    break;
                case 'g':
                    int res = queue.pop();
                    System.out.println("取出的数据为:" + res);
                    break;
                case 'h':
                    int h = queue.head();
                    System.out.println("队列头元素为:" + h);
                    break;
                case 'l':
                    int len = queue.length();
                    System.out.println("队列长度为:" + len);
                    break;
                case 's':
                    System.out.println("队列中的数据为:");
                    queue.show();
                    break;
                case 'e':
                    scanner.close();
                    flag = false;
                    break;
                default:
                    break;
            }
        }
    }
}
  • 测试结果
原文地址:https://www.cnblogs.com/sinlearn/p/13588160.html