CodesJava

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.

Example

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.");
            increaseCapacity();
        } else {
            rear++;
            if(rear == capacity-1){
                rear = 0;
            }
            queueArray[rear] = item;
            currentSize++;
            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 {
            front++;
            if(front == capacity-1){
                System.out.println("Removed element: "+queueArray[front-1]);
                front = 0;
            } else {
                System.out.println("Removed element: "+queueArray[front-1]);
            }
            currentSize--;
        }
    }
 
    /**
     * 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;
        while(true){
            newArr[++index] = this.queueArray[tmpFront];
            tmpFront++;
            if(tmpFront == this.queueArray.length){
                tmpFront = 0;
            }
            if(currentSize == index+1){
                break;
            }
        }
        //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);
	        queue.enqueue(41);
	        queue.dequeue();
	        queue.enqueue(6);
	        queue.enqueue(24);
	        queue.enqueue(7);
	        queue.enqueue(4);
	        queue.enqueue(45);
	        queue.dequeue();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output

Adding element at front: 134
[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]
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki