数据结构-数组实现循环队列-Java版

数组实现循环队列(Java版)(附代码)

什么是队列?

先进先出的线性数据结构

什么是循环队列?

为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。(来源:百度百科)

什么是取磨操作?

本章循环队列使用数组取磨操作实现头尾下标位置变化。
取模操作:%
例如:5%3=2 3%6=3

  • 循环队列各操作图示
    循环队列
  • 代码实现

队列接口

package study.data.queue;

public interface  MyQueue {


    /**
     * 补充接口小知识:
     * 1.接口中的方法会被隐式的指定为  public abstract (只能是 public abstract,其他修饰符都会报错)。
     * 2.接口中的变量会被隐式的指定为  public static final   变量(并且只能是 public,用 private 修饰会报编译错误。)
     * @param i
     */

    // 判断队列是否为空
    boolean isEmpty();

    // 判断队列是否满了
    boolean isFull();

    // 添加
    void addQueue(int i);

    // 取出元素
    int getQueue();

    // 遍历队列
    void printQueue();

    // 获取第一个元素
    void headQueue();
}

循环队列实现

package study.data.queue;

/**
 * 循环队列
 */
public class CircularQueue implements MyQueue{

    protected int maxSize;
    protected int head;
    protected int tail;
    protected int[] arr;

    public CircularQueue(int size){
        this.maxSize = size;
        this.head = 0;
        this.tail = 0;
        this.arr = new int[this.maxSize];
    }

    /**
     * 判断队列是否为空
     * 为空条件:head==tail
     * @return
     */
    @Override
    public boolean isEmpty() {
        return head == tail;
    }

    /**
     * 判断队列是否满了
     * 队列满条件:
     * @return
     */
    @Override
    public boolean isFull() {
        return (tail+1)%maxSize == head;
    }

    @Override
    public void addQueue(int i) {
        if (isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[tail] = i;
        tail = (tail+1)%maxSize;
    }

    @Override
    public int getQueue() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int value = arr[head];
        head = (head+1)%maxSize;
        return value;
    }

    @Override
    public void printQueue() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        System.out.print("队列为:");
        for (int i = head; i < size()+head; i++) {
            System.out.print(arr[i%maxSize]+"  ");
        }
        System.out.println();
        System.out.print("下标为:");
        for (int i = head; i < head+size(); i++) {
            System.out.print(i%maxSize+"   ");
        }
        System.out.println();
    }

    /**
     * 获取队列的有效数据长度
     */
    private int size(){
        return (tail+maxSize-head)%maxSize;
    }

    @Override
    public void headQueue() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        System.out.println("队头是:"+arr[head]);
    }
}

测试类

package study.data.queue;

import java.util.Scanner;

public class QueueTest {

    public static void main(String[] args) {
        //MyQueue queue = new MyBugQueue(3);
        MyQueue queue = new CircularQueue(3);

        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        char input ;
        while (flag){
            System.out.println("入队(a)");
            System.out.println("出队(g)");
            System.out.println("遍历(s)");
            System.out.println("偷看(h)");
            input = scanner.next().charAt(0);
            switch (input){
                case 'a':
                    System.out.print("请输入一个数:");
                    queue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    System.out.println("出队元素为:"+queue.getQueue());
                    break;
                case 's':
                    queue.printQueue();
                    break;
                case 'h':
                    queue.headQueue();
                    break;
                default:
                    break;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

难打的仗

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值