Java 1.8使用数组实现循环队列

作者:因吉 时间:2022-02-11 04:00:10 

本文实例为大家分享了Java 1.8使用数组实现循环队列的具体代码,供大家参考,具体内容如下

1、引入

使用数组实现循环队列,功能如下:

1)isFull():队列满?
2)isEmpty():队列空?
3)add():添加元素。
4)pop():移除元素。
5)display():展示队列。
6)getSize():获取当前队列元素个数。

2、代码


package DataStructure;

import java.util.Arrays;

/**
* @author: Inki
* @email: inki.yinji@qq.com
* @create: 2020 1022
* @last_modify: 2020 1023
*/

public class MyArrayQueue<AnyType> {

/**
 * The default max size of my array queue.
 */
private final int DEFAULT_MAX_SIZE = 10;

/**
 * The max size of my array queue.
 */
private int maxSize;

/**
 * The front of my array queue.
 */
private int front;

/**
 * The rear of my array queue.
 */
private int rear;

/**
 * Using array to simulate queue.
 */
private AnyType[] arrQueue;

/**
 * The first constructor.
 */
public MyArrayQueue() {
 this(DEFAULT_MAX_SIZE);
}//Of the first constructor

/**
 * The second constructor.
 */
public MyArrayQueue(int paraMaxSize) {
 maxSize = paraMaxSize + 1;
 arrQueue = (AnyType[]) new Object[maxSize];
 front = 0;
 rear = 0;
}//Of the second constructor

/**
 * Queue is full?
 * @return:
 *  True if full else false.
 */
public boolean isFull() {
 return (rear + 1) % maxSize == front;
}//Of isFull

/**
 * Queue is empty?
 * @return:
 *  True if empty else false.
 */
public boolean isEmpty() {
 return front == rear;
}//Of isEmpty

/**
 * Add element.
 * @param:
 *  paraVal:
 *   The given value.
 */
public void add(AnyType paraVal) {
 if(isFull()) {
  System.out.println("The queue is full.");
  return;
 }//Of if
 arrQueue[rear] = paraVal;
 rear = (rear + 1) % maxSize;
}//Of add

/**
 * Pop element.
 */
public AnyType pop() {
 if (isEmpty()) {
  throw new RuntimeException("The queue is full.");
 }//Of if
 AnyType retVal = arrQueue[front];
 front = (front + 1) % maxSize;
 return retVal;
}//of pop

/**
 * Display array queue.
 */
public void display() {
 if (isEmpty()) {
  System.out.println("The queue is empty.");
  return;
 }//Of if

System.out.print("The queue is: [");
 int i = front;
 while (i != (rear + maxSize- 1) % maxSize) {
  System.out.printf("%s, ", arrQueue[i]);
  i = (i + 1) % maxSize;
 }//Of while
 System.out.printf("%s]", arrQueue[rear - 1]);
}//Of display

/**
 * Get current size of my array queue.
 */
public int getSize() {
 return (rear - front + maxSize) % maxSize + 1;
}//Of getSize

/**
 * The main
 **/
public static void main(String[] args) {
 MyArrayQueue <Integer> testArrayQueue = new MyArrayQueue<>(3);
 testArrayQueue.add(1);
 testArrayQueue.add(2);
 testArrayQueue.add(4);
 testArrayQueue.pop();
 testArrayQueue.display();
}//Of main

}//Of MyArrayQueue

来源:https://blog.csdn.net/weixin_44575152/article/details/109240542

标签:java,循环队列
0
投稿

猜你喜欢

  • Java如何根据不同系统动态获取换行符和盘分割符

    2022-02-27 10:33:10
  • Java定位问题线程解析

    2023-08-09 22:04:27
  • 详细解读JAVA多线程实现的三种方式

    2022-01-14 04:35:31
  • Java实现文件读取和写入过程解析

    2023-06-28 11:35:36
  • java GUI实现学生图书管理简单实例

    2023-11-11 05:00:05
  • Flutter倒计时/计时器的实现代码

    2023-07-01 03:50:50
  • java web服务器实现跨域访问

    2023-09-17 06:55:08
  • 关于eclipse中运行tomcat提示端口被占用的4种解决

    2022-04-15 10:56:12
  • Java fastdfs客户端实现上传下载文件

    2023-11-26 09:45:41
  • synchronized背后的monitor锁实现详解

    2023-07-31 08:14:10
  • java GUI编程之paint绘制操作示例

    2023-11-24 17:58:39
  • Flutter 队列任务的实现

    2023-07-07 17:25:14
  • android调试工具DDMS的使用详解

    2023-06-21 09:06:22
  • Bootstrap 下拉菜单.dropdown的具体使用方法

    2023-07-08 19:10:46
  • java实现截取PDF指定页并进行图片格式转换功能

    2023-08-24 02:58:56
  • Java JDBC导致的反序列化攻击原理解析

    2023-09-24 15:38:42
  • Hibernate一级缓存和二级缓存详解

    2023-11-16 11:58:11
  • Java设计模式之命令模式

    2022-06-17 22:49:07
  • springboot配置文件绑定实现解析

    2022-06-07 23:32:38
  • Windows编写jar启动脚本和关闭脚本的操作方法

    2021-05-28 04:36:58
  • asp之家 软件编程 m.aspxhome.com