Queue Implementation in Java using Array

Filed Under: Java

What is a Queue?

Queue is a special type of data structure, which is designed to hold elements before processing and process elements in a FIFO (first-in-first-out) manner. It’s part of java collections framework. In this tutorial, we will learn Queue implementation in Java using an array.

Basic Queue Functions

A Queue must have the following functions:

  • enqueue(obj) – insert element to the queue.
  • dequeue() – remove and return the least recent item from the queue.
  • isEmpty() – returns true if the queue is empty, else false.

Queue Implementation in Java

We can implement basic Queue functions using an array.

Here is the complete code to implement a Queue in Java.

package com.journaldev.java;

public class MyQueue {

	public static final int DEFAULT_SIZE = 5;

	private Object data[];

	private int index;

	public MyQueue() {
		data = new Object[DEFAULT_SIZE];
	}

	public MyQueue(int size) {
		data = new Object[size];
	}

	public boolean isEmpty() {
		return index == 0;
	}

	public void enqueue(Object obj) throws Exception {
		if (index == data.length - 1) {
			throw new Exception("Queue is full. Dequeue some objects");
		}
		this.data[index] = obj;
		this.index++;
	}

	public Object dequeue() throws Exception {
		if (isEmpty())
			throw new Exception("Queue is empty");
		Object obj = this.data[0];
		for (int i = 0; i < this.index - 1; i++) {
			data[i] = data[i + 1];
		}
		this.index--;
		return obj;

	}
}

Important Points

  • There are two constructors – one to create a Queue with default size, the other one is used to specify the queue size.
  • We are using a private integer variable “index” to manage the queue elements.
  • We are using an Object array so that we can hold any type of object in the Queue. We can use generics here too but I am avoiding that to keep the program simple.
  • If the queue is full, the enqueue() will throw an exception with proper message.
  • If the queue is empty and we call the dequeue() function, an exception is raised.

Implementation Limitations
The Queue methods are not synchronized and it’s not thread-safe. There is a chance of data inconsistency if you use this implementation in a multi-threaded environment.

Test Program to Check Queue Implementation

Let’s test our queue implementation with some calls to enqueue() and dequeue() functions.

MyQueue queue = new MyQueue();
queue.enqueue("1");
System.out.println(queue.dequeue());

queue.enqueue("2");
queue.enqueue("3");
queue.enqueue("4");
System.out.println(queue.dequeue());

queue.enqueue("5");
queue.enqueue("6");
// queue.enqueue("7");
// queue.enqueue("8");

Output:

1
2

If you uncomment the last two lines, the queue will get full and an exception will be raised.

Exception in thread "main" java.lang.Exception: Queue is full. Dequeue some objects
	at com.journaldev.java.MyQueue.enqueue(MyQueue.java:25)
	at com.journaldev.java.MyQueue.main(MyQueue.java:56)

Conclusion

A Queue is one of the simplest data structures. We can implement it using an array or a list.

Comments

  1. bharath kumar reddy says:

    write a java program to create a class called queue and implementation to below methods
    (i)insert (ii)delete (iii)size (iv)display (v)clear

  2. nawaf says:

    2)Implement a program to automate a car showroom. The program will maintain a list of car models, their price, name of manufacturer, engine capacity etc. The menu should allow insertion of information related to new car models, delete obsolete models, answering queries such as listing all car models within a price range specified by the user and listing all the details given a car model. The program can be implemented using any of the data structure you have studied in this course.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages