CodesJava

Easy learning with example program codes

Java 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.

Examples

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.");
        } 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;
    }
 
	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.dequeue();
	        queue.dequeue();
	        queue.enqueue(4);
	        queue.dequeue();
	        queue.enqueue(918);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
}

Output

Element 41 is pushed to Queue.
Removed element: 41
Element 6 is pushed to Queue.
Element 24 is pushed to Queue.
Element 7 is pushed to Queue.
Removed element: 6
Removed element: 24
Element 4 is pushed to Queue.
Removed element: 7
Element 918 is pushed to Queue.
Sign Up/ Sign In
Ask a Question


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 hr@codesjava.com.

Development

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

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