队列的概念及实现

队列的概念及实现

  1. 队列是一个有序列表,可以用数组或链表来实现。遵循先进先出原则,先入队的数据,优先取出。队列的应用场景如银行排队等。

  2. 使用数组模拟队列

import java.util.Scanner;

public class MyArrayQueue {
    
    //容量
    private int capacity;
    
    //队头
    private int front;
    
    //队尾
    private int rear;
    
    //存放队列的数据
    private int[] arr;
    
    MyArrayQueue(int capacity){
        this.capacity = capacity;
        this.arr = new int[capacity];
        this.front = -1; //指向队头的前一个位置
        this.rear = -1;  //指向队尾
    }
    
    boolean isFull() {
        return rear == capacity - 1;
    }
    
    boolean isEmpty() {
        return rear == front;
    }
    
    /**
     * 从队尾入队
     * @param data 数据
     */
    void addQueue(int data) {
        if (isFull()) {
            System.out.println("队列满,不能加入数据");
            return;
        }
        arr[++rear] = data;
    }
    
    /**
     * 从队头出队
     */
    int getData() {
        if (isEmpty()) {
            throw new RuntimeException("队列空,不能取数据");
        }
        return arr[++front];
    }
    
    void showQueue() {
        if(isEmpty()){
            return;
        }

        for(int i=0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }
    
    /**
     * 队头元素
     */
    int peek() {
        if(isEmpty()){
            throw new RuntimeException("队列空,没有数据");
        }
        return arr[front+1];
    }
    
    public static void main(String[] args) {
        MyArrayQueue queue = new MyArrayQueue(3);
        Scanner input = new Scanner(System.in);
        boolean loop = true;
        char key = ' ';
        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 = input.next().charAt(0);
            switch(key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = input.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getData();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.peek();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    input.close();
                    loop = false;
                default:
                    break;
            }
        }

        System.out.println("程序退出");
    }
}
  1. 环形队列,避免假溢出,需要牺牲空间
import java.util.Scanner;

public class CircleQueue {
    
    private int maxSize;
    
    //指向队列的第一个元素
    private int front;
    
    //指向队列的最后一个元素的后一个元素
    private int rear;
    
    private int[] arr;
    
    CircleQueue(int size) {
        maxSize = size;
        arr = new int[maxSize];
    }
    
    boolean isEmpty(){
        return rear == front;
    }

    boolean isFull(){
        return (rear+1)%maxSize == front;
    }
    
    void addQueue(int data){
        if(isFull()){
            return;
        }
        arr[rear] = data;
        rear = (rear+1)%maxSize;
    }

    int getData(){
        if(isEmpty()){
            throw new RuntimeException("队列空,不能取数据");
        }

        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }

    void show(){
        if(isEmpty()){
            return;
        }

        for(int i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
        }
    }

    int size(){
        //判断队列中有效元素的个数
        return (maxSize + rear - front)%maxSize;
    }

    int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列空,没有数据");
        }
        return arr[front];
    }
    
    public static void main(String[] args) {
        System.out.println("测试数组模拟环形队列");
        CircleQueue queue = new CircleQueue(4);
        Scanner input = new Scanner(System.in);
        boolean loop = true;
        char key;
        while(loop) {
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查询队列头的数据");
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            key = input.next().charAt(0);
            switch(key){
                case 's':
                    queue.show();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = input.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getData();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.peek();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    input.close();
                    loop = false;
                default:
                    break;
            }
        }

        System.out.println("程序退出");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值