java数据结构(二)--队列

 数组模拟环形队列

    将数组看做是一个环形的。(通过取模的方式来实现即可)
    分析说明:
  1) 尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在做判断队列满的
      时候需要注意(rear + 1) % maxSize == front 满]
  2) rear == front [空]
  3) 分析示意图:

 代码实现

package com.zjf.datastruct.queque;

import java.util.Scanner;

/**
 * 数组模拟环型队列
 */
public class Queque {

    public static void main(String[] args) {
        //测试一把
        System.out.println("测试数组模拟环形队列的案例~~~");
        // 创建一个环形队列
        CircleQueue queue = new CircleQueue(5); //说明:数组长度设置5, 其队列的有效数据最大是4
        char key = ' '; // 接收用户输入
        Scanner scanner = 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 = scanner.next().charAt(0);// 接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addData(value);
                    break;
                case 'g': // 取出数据
                    try {
                        int res = queue.getData();
                        System.out.printf("取出的数据是%d
", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.showFirstData();
                        System.out.printf("队列头的数据是%d
", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");
    }
}

class CircleQueue {
    private Integer maxSize; //数组的最大长度
    private Integer front; //指向队列的第一个元素 初始值=0
    private Integer real;  //指向队列最后一个元素的后一个位置  初始值=0
    private int[] arr;     //保存队列元素

    /**
     * 初始化队列
     *
     * @param maxSize
     */
    public CircleQueue(Integer maxSize) {
        this.maxSize = maxSize;
        this.front = 0;
        this.real = 0;
        arr = new int[maxSize];
    }

    /**
     * 判断队列是否满
     */
    public boolean isFull() {
        if ((real + 1) % maxSize == front) {
            return true;
        }
        return false;
    }

    /**
     * 判断队列是否为空
     */
    public boolean isEmpty() {
        if (real == front) {
            return true;
        }
        return false;
    }

    /**
     * 添加数据
     */
    public void addData(Integer data) {
        //队列是否已满
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        arr[real] = data;
        //将real 后移, 这里必须考虑取模
        real = (real + 1) % maxSize;
    }

    /**
     * 取数据
     */
    public int getData() {
        //队列是否为空
        if (isEmpty()) {
            System.out.println("队列为空");
           throw new RuntimeException("队列为空");
        }
        Integer value = arr[front];
        //将front 后移, 考虑取模
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 查看队列第一个数据
     */
    public int showFirstData() {
        //队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }

    /**
     * 求出当前队列有效数据的个数
     *
     * @return
     */
    public int size() {
        return (real + maxSize - front) % maxSize;
    }

    /**
     * 查看队列中的数据
     */
    public void showQueue() {
        //队列是否为空
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        // 思路:从front 开始遍历,遍历多少个元素
        // 动脑筋
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d
", i % maxSize, arr[i % maxSize]);
        }
    }


}
原文地址:https://www.cnblogs.com/wiliamzhao/p/14623085.html