美文网首页
3、队列

3、队列

作者: 黑白小兔 | 来源:发表于2021-02-14 17:18 被阅读0次

    先进先出的原则

    简单的队列实现

    package com.vicbaily.queue;public class ArrayQueueDemo { public static void main(String[] args) { int maxSize = 3 ; ArrayQueue queue = new ArrayQueue(maxSize); for (int index = 0; index < maxSize; index++) { double value = Math.random(); System.out.print(value + "\t"); queue.addQueue(value); } System.out.println(); System.out.println("=================================="); for (int index = 0; index < maxSize; index++) { System.out.print(queue.getQueue() + "\t"); } }}class ArrayQueue { private int maxSize; private int front; private int rear; private double[] arr; public ArrayQueue(int maxSize) { this.maxSize = maxSize; arr = new double[maxSize]; front = -1; rear = -1; } public boolean isFull(){ return maxSize == rear + 1; } public boolean isEmpty() { return rear == front; } public void addQueue(double n) { if (isFull()) { System.out.println("队列已经满了"); return; } rear++; arr[rear] = n; } public double getQueue() { if (isEmpty()) { throw new RuntimeException("队列为空"); } front++; return arr[front]; } public void showQueue() { if (isEmpty()) { System.out.println("队列为空"); return; } for (int index = 0; index < arr.length; index++) { System.out.printf("%d\t", arr[index]); } } public double headQueue() { if (isEmpty()) { throw new RuntimeException("队列为空"); } return arr[front + 1]; }}

    相关文章

      网友评论

          本文标题:3、队列

          本文链接:https://www.haomeiwen.com/subject/jwpyxltx.html