zoukankan      html  css  js  c++  java
  • Implement Circular Queue using Array

    package com.javainuse;
    
    import java.util.Arrays;
    
    public class CircularQueueImplementation {
    
        public static void main(String[] args) {
    
            CircularQueue<Integer> circularQueue = new CircularQueue(8);
    
            circularQueue.enqueue(15);
            circularQueue.enqueue(16);
            circularQueue.enqueue(17);
            circularQueue.enqueue(18);
            circularQueue.enqueue(19);
            circularQueue.enqueue(20);
            circularQueue.enqueue(21);
            circularQueue.enqueue(22);
    
            System.out.println("Full Circular Queue" + circularQueue);
    
            System.out.print("Dequeued following element from circular Queue ");
            System.out.println(circularQueue.dequeue() + " ");
            circularQueue.enqueue(23);
            System.out.println("After enqueueing circular queue with element having value 23");
            System.out.println(circularQueue);
        }
    
    }
    
    //implementation of Circular Queue using Generics
    class CircularQueue<E> {
    
        private int currentSize; //Current Circular Queue Size
        private E[] circularQueueElements;
        private int maxSize; //Circular Queue maximum size
    
        private int rear;//rear position of Circular queue(new element enqueued at rear).
        private int front; //front position of Circular queue(element will be dequeued from front).      
    
        public CircularQueue(int maxSize) {
            this.maxSize = maxSize;
            circularQueueElements = (E[]) new Object[this.maxSize];
            currentSize = 0;
            front = 0;
            rear = 0;
        }
    
        /**
         * Enqueue elements to rear.
         */
        public void enqueue(E item) throws QueueFullException {
            if (isFull()) {
                throw new QueueFullException("Circular Queue is full. Element cannot be added");
            }
            else {
                circularQueueElements[rear] = item;
                rear = (rear + 1) % circularQueueElements.length;
                currentSize++;
            }
        }
    
        /**
         * Dequeue element from Front.
         */
        public E dequeue() throws QueueEmptyException {
            E deQueuedElement;
            if (isEmpty()) {
                throw new QueueEmptyException("Circular Queue is empty. Element cannot be retrieved");
            }
            else {
                deQueuedElement = circularQueueElements[front];
                circularQueueElements[front] = null;
                front = (front + 1) % circularQueueElements.length;
                currentSize--;
            }
            return deQueuedElement;
        }
    

      

  • 相关阅读:
    用 Go 实现一个 LRU cache
    【转】入行软件测试,零基础拿OFFER
    【转】IntelliJ idea 高效使用教程,一劳永逸!
    python连接Oracle报错DPI1047
    【转】Pycharm快捷键设置(鼠标滚动控制字体大小)
    【转】Ubuntu:命令行安装可视化界面
    【转】Windows 如何在cmd命令行中查看、修改、删除与添加环境变量
    VAR多变量预测
    windows进程管理
    git关闭filemode
  • 原文地址:https://www.cnblogs.com/apanda009/p/7927447.html
Copyright © 2011-2022 走看看