Publish Your Article Here

Dynamic Queue implementation using arrays

A queue is a kind of abstract data type or collection in which the entities in the collection are kept in order and the only operations on the collection are the addition of entities to the rear terminal position, called as enqueue, and removal of entities from the front terminal position, called as dequeue. The queue is called as First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. You can find more details in the previous example

Here you can find dynamic queue implementation based on array. The capacity of the array will be increased when the queue is full.

package com.java2novice.ds.queue;

public class DynamicQueueImpl {

        private int capacity = 2;
        int queueArr[];
        int front = 0;
        int rear = -1;
        int currentSize = 0;
        public DynamicQueueImpl(){
                queueArr = new int[this.capacity];

         * this method adds element at the end of the queue.
         * @param item
        public void enqueue(int item) {
                if (isQueueFull()) {
                        System.out.println("Queue is full, increase capacity...");
                if(rear >= queueArr.length && currentSize != queueArr.length){
                        rear = 0;
                queueArr[rear] = item;
                System.out.println("Adding: " + item);

         * this method removes an element from the top of the queue
        public void dequeue() {
                if (isQueueEmpty()) {
                        System.out.println("Underflow ! Unable to remove element from Queue");
                } else {
                        if(front > queueArr.length-1){
                                System.out.println("removed: "+queueArr[front-1]);
                                front = 0;
                        } else {
                                System.out.println("removed: "+queueArr[front-1]);

         * This method checks whether the queue is full or not
         * @return boolean
        public boolean isQueueFull(){
                boolean status = false;
                if (currentSize == queueArr.length){
                        status = true;
                return status;
         * This method checks whether the queue is empty or not
         * @return
        public boolean isQueueEmpty(){
                boolean status = false;
                if (currentSize == 0){
                        status = true;
                return status;
        private void increaseCapacity(){
                //create new array with double size as the current one.
                int newCapacity = this.queueArr.length*2;
                int[] newArr = new int[newCapacity];
                //copy elements to new array, copy from rear to front
                int tmpFront = front;
                int index = -1;
                        newArr[++index] = this.queueArr[tmpFront];
                        if(tmpFront == this.queueArr.length){
                                tmpFront = 0;
                        if(currentSize == index+1){
                //make new array as queue
                this.queueArr = newArr;
                System.out.println("New array capacity: "+this.queueArr.length);
                //reset front & rear values
                this.front = 0;
                this.rear = index;
        public static void main(String a[]){
                DynamicQueueImpl queue = new DynamicQueueImpl();

Adding: 4
removed: 4
Adding: 56
Adding: 2
Queue is full, increase capacity...
New array capacity: 4
Adding: 67
removed: 56
Adding: 24
Adding: 98
removed: 2
removed: 67
removed: 24
Adding: 435
removed: 98
removed: 435
<< Previous Program | Next Program >>

List of Queue Data Structure Examples

  1. Queue introduction & array based implementation
  2. Dynamic Queue implementation using arrays
  3. Double-ended queue (Decue) Implementation
  4. Double-ended queue (Decue) implementation using Doubly linked list
  5. Priority Queue introduction and Java implementation
Knowledge Centre
Transient and Volatile Modifiers
Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.