av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java 1.8使用數組實現循環隊列

瀏覽:5日期:2022-08-21 18:52:43

本文實例為大家分享了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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 国产成人99久久亚洲综合精品 | 欧美亚洲三级 | 日韩欧美中文字幕在线观看 | 欧美一级在线播放 | 欧美一区二区在线 | 精品视频一区二区三区 | 日本免费不卡视频 | 亚洲欧美综合网 | 亚洲国产精品va在线看黑人 | 国产欧美在线观看 | 日韩怡红院 | 亚洲精品欧美 | 欧美一级片在线 | 少妇搡bbbb搡bbb搡毛茸茸 | 国产精品福利一区 | 草草在线视频 | 国产精品久免费的黄网站 | 特级西西444www大精品视频 | 国产一级生活片 | 精品国产区一区二 | 五月激情综合 | 三级黄色片网站 | 五月婷婷丁香花 | 国产69精品久久久久久 | www.国产精品.com | 午夜精品视频 | 日日夜夜精品视频免费 | 国产免费黄色片 | 日韩国产精品视频 | 午夜在线观看视频 | 日韩精品在线一区 | 国产999视频 | 中文字幕理伦片免费看 | 国产激情在线视频 | 视频一区二区在线 | 黑人精品xxx一区一二区 | 成人黄色小视频 | 全部免费毛片在线播放高潮 | 成人激情视频在线观看 | 午夜成人在线视频 | 亚洲毛片av|