数据结构与算法--数组模拟队列(Queue)

此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢!


一、概述

需要强调的是,此文所指的队列并非之后的环形队列、循环队列、链队列等,就是最普通的用数组模拟的顺序队列

队列的基本组成结构为:

  • (int) maxSIze:队列的最大容量。
  • (int) front:指向队列头的“指针”。(实际上存储的是指向队列第一个元素的前一个位置的下标
  • (int) rear:指向队列尾的“指针”。(区别于front,所存储的就是队列最后一个元素的位置下标
  • (E[ ]) queueArr:模拟队列的数组。(E的类型取决于实际情况)

在这里插入图片描述
(从上图其实也可以看出普通数组队列的缺陷)

内容

  • 构造方法创建数组队列。
  • isEmpty(), isFull()【判断队列是否为空/满】
  • addQueue()【向队列中添加元素】
  • getQueue()【获取队列头元素并将front向后移一个,实现“假出队列”的效果】
  • peekQueue()【获取队列头元素但不影响front的值】
  • showQueue()【展示队列所有元素】

二、代码实现

  • Attributes and constructor
/*
 ArrayQueue

 Zzay

 2021/01/14
 */
package com.zzay.queue;

/**
 * TIPS:
 * (1) "front" indicator points to the previous place of the first element,
 * and it changes with the output of the queue.
 * (2) "rear" indicator points to the last element, and it changes with the input of the queue.
 *
 * PROBLEMS:
 * (1) This kind of queue can be used only once. It cannot provide the function of reusing.
 *
 * SOLUTION:
 * (1) Using a certain algorithm to convert this queue into a circular queue.
 *
 * @author Zzay
 * @version 2021/01/14
 */
public class ArrayQueue {

    // The maximum capacity of the array.
    private int maxSize;

    // The indicator of the front element of the queue.
    // The front indicator points to the previous place of the first elements.
    private int front;

    // The indicator of the rear element of the queue.
    // The rear indicator includes the last element.
    private int rear;

    // The array that modifies a queue.
    private int[] queueArr;

    /**
     * Receive a capacity and instantiate a queue array with the max size of the same value.
     * Also, do initializations.
     *
     * @param capacity The expected max size of the queue array
     */
    public ArrayQueue(int capacity) {
        if (capacity <= 0) {
            System.out.println("Invalid capacity, enter again...");
            return;
        }
        maxSize = capacity;
        queueArr = new int[maxSize];
        initialize();
    }
    
    /**
     * Initialize the front and rear indicator of the queueArr.
     */
    private void initialize() {
        front = -1;
        rear = -1;
    }
}
  • Methods
    /**
     * Judge whether the queue is empty or not.
     *
     * @return True if the queue is empty; false if the queue is not empty
     */
    public boolean isEmpty() {
        if (front == rear) {
            return true;
        }
        return false;
    }

    /**
     * Judge whether the queue is full or not.
     *
     * @return True if the queue is full; false if the queue is not full
     */
    public boolean isFull() {
        if (rear == maxSize - 1) {
            return true;
        }
        return false;
    }

    /**
     * Add an element into the queue if it's not full.
     */
    public void addQueue(int data) {
        //Judge if the queue is full or not.
        if (isFull()) {
            System.out.println("The queue is full, cannot add new element...");
            return;
        }
        rear++;
        queueArr[rear] = data;
    }

    /**
     * Get the first element from the queue if it's not empty.
     *
     * @return The first element in the queue
     */
    public int getQueue() {
        //Judge if the queue is empty or not.
        if (isEmpty()) {
            throw new RuntimeException("The queue is empty, cannot get element from it...");
        }
        front++;
        return queueArr[front];
    }

    /**
     * Get the data of the first element in the queue, without affecting its existence.
     */
    public int peekQueue() {
        if (isEmpty()) {
            throw new RuntimeException("The queue is empty, cannot peek the first element...");
        }
        return queueArr[front + 1];
    }

    /**
     * Display the data in the queue.
     */
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("The queue is empty...");
            return;
        }
        int count = 0;
        for (int i = front + 1; i < rear + 1; i++) {
            System.out.printf("array[%d]: %d\n", count++, queueArr[i]);
        }
        System.out.println();
    }
  • Test
/*
 ArrayQueue

 Zzay

 2021/01/14
 */
package com.zzay.queue;


import java.util.Scanner;

/**
 * @author Zzay
 * @version 2021/01/14
 */
public class ArrayQueueTest {

    public static void main(String[] args) {
        char key = ' ';
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        ArrayQueue arrayQueue = new ArrayQueue(3);
        
        while (loop) {
            System.out.println("s(show): show queue");
            System.out.println("a(add): add element");
            System.out.println("g(get): get first element");
            System.out.println("p(peek): peek first element");
            System.out.println("e(exit): exit the program");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("Please enter the element you'd like to add:");
                    arrayQueue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        System.out.println("The element is: " + arrayQueue.getQueue());
                        System.out.println();
                    } catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'p':
                    try {
                        System.out.println("The first element is: " + arrayQueue.peekQueue());
                        System.out.println();
                    } catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("Thanks for your using!");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值