Easy learning with example program codes

Java dynamic queue implementation

A queue is an ADT – Abstract Data Type or a linear data structure. It is a FIFO data structure because element inserted first will be removed first. FIFO stands for First-in-first-out. Queue use one end to insert data which is called REAR or tail and other end to remove the data which is called FRONT or head.

Queue operations

  • enqueue(): Insert an item to the queue.
  • dequeue(): Remove an item from the queue.
  • peek(): Get the top data element of the queue, without removing it.
  • isFull(): Check if queue is full.
  • isEmpty(): Check if queue is empty.

Queue states

  • Overflow state: A queue is in overflow state if it does not contain enough space to accept an entity to be inserted.
  • Underflow state: A queue is in underflow state if we want to operate stack with dequeue operation and the queue is empty.

Dynamic Queue

As we discussed in previous example that a queue throws an exception if enough space is not available to accept an element to be pushed. To overcome this situation we can create dynamic queue whose capacity will keep increases as it reaches to max capacity.


package com.codesjava;
public class Test {
    private int capacity;
    int queueArray[];
    int front = 0;
    int rear = -1;
    int currentSize = 0;
    public Test(int queueSize){
        this.capacity = queueSize;
        queueArray = new int[this.capacity];
     * Adds element at the end of the queue.
     * @param item
    public void enqueue(int item) {
        if (isQueueFull()) {
            System.out.println("Overflow state. Increase capacity.");
        } else {
            if(rear == capacity-1){
                rear = 0;
            queueArray[rear] = item;
            System.out.println("Element " + item+ " is pushed to Queue.");
     * Removes an element from the top of the queue
    public void dequeue() {
        if (isQueueEmpty()) {
            System.out.println("Underflow state.");
        } else {
            if(front == capacity-1){
                System.out.println("Removed element: "+queueArray[front-1]);
                front = 0;
            } else {
                System.out.println("Removed element: "+queueArray[front-1]);
     * Checks whether the queue is full or not
     * @return boolean
    public boolean isQueueFull(){
        boolean status = false;
        if (currentSize == capacity){
            status = true;
        return status;
     * 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.queueArray.length*2;
        int[] newArr = new int[newCapacity];
        //Copy elements to new array
        int tmpFront = front;
        int index = -1;
            newArr[++index] = this.queueArray[tmpFront];
            if(tmpFront == this.queueArray.length){
                tmpFront = 0;
            if(currentSize == index+1){
        //Convert new array as queue
        this.queueArray = newArr;
        System.out.println("New array capacity: "+this.queueArray.length);
        //Reset front and rear values
        this.front = 0;
        this.rear = index;
	public static void main(String args[]){
	  try {
		Test queue = new Test(4);
		} catch (Exception e) {


Adding element at front: 134
Adding element at front: 14
[14, 134]
Adding element at front: 13
[13, 14, 134]
Remove element from front: 13
[14, 134]
Adding element at rear: 455
[14, 134, 455]
Remove element from front: 14
[134, 455]

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki