Queue Data Structure Implementation in Java using an Array

A queue is a linear data structure that works on the First in First out principle. A element insert at one and removes from another end. A Stack have one open end while the queue has two open ends.

java queue implementation

Recommended: Stack Implementation in Java using an Array

Real Time Applications

There are plenty of real world applications where the queue is largely used.

For example, When you press a key each time from your keyboard to write a word. These keywords will get into the queue as a sequence and they display on the screen as to how they were entered. 

A message queue is a concept used to communicate between processes. When a sender sends a message to a recipient if the recipient does not receive the message maybe because it is busy with some process or the receiver is offline, the message instead of dropping off, wait in a certain kind of queue when the receiver is ready then the message is removed from the queue.

In the Operating system, processes are scheduled based on the queue and execute accordingly.

Various Operation in Queue

Enqueue: Adds an element to the queue. if the queue is full then it is said to be overflow.
Dequeue: Removes an element from the queue. if the queue is empty then it is said to be underflow. the elements are removed in which they are inserted.
Front: Returns the front element of the queue
Rear: Returns the last element of the queue.

Queue data structure implementation

//queue implementation using array
public class Queue {
	int capacity;
	int front, rear, size;
	int array[];
	//Initialize instance variables
	public Queue(int capacity) {
		this.capacity = capacity;
		this.front = this.size = 0;
		this.rear = this.capacity - 1;
		array = new int[capacity];		
	//check the queue is full
	boolean isFull(Queue queue) {
		//size of the queue and queue capacity are equal 
		// than queue is full
		return (queue.size == queue.capacity);
	//empty queue check
	boolean isEmpty(Queue queue) {
		//empty queue size is zero
		return (queue.size == 0);
	//adding element to queue
	public void enqueue(int item) {
		//checking queue is full
		if(isFull(this)) {
			System.out.println("Queue is full");
		//rear modulo capacity returns reminder
		//which will be the index of the array and
		//where item will be added
		rear = (rear + 1) % capacity;
		array[rear] = item;
		//increasing size
		size = size + 1;
		System.out.println(item + " is added to queue");
	//deleting element from the queue
	public int dequeue() {
		//checking the empty queue and return integer negative value
		if(isEmpty(this)) {
			return Integer.MIN_VALUE;
		//size is the front value which is the index of the array.
		// it fetch the front element  of the queue
		int item = array[front];
		//increase the front value to get next queued element in queue 
		//modulo return reminder and front value will be pointing to next value
		front = (front + 1) % capacity;
		//decrease the size
		size = size - 1;
		return item;
	//get front value
	int front() {
		if(isFull(this)) {
			return Integer.MIN_VALUE;
		//return front index value as front
		return array[front];
	//get rear value
	int rear() {
		if(this.isEmpty(this)) {
			return Integer.MIN_VALUE;
		//return rear index value as rear
		return array[rear];
	//driver class
	public static void main(String[] args) {
		Queue queue = new Queue(10);
		System.out.println(queue.dequeue() + " removed from the queue");
		System.out.println("The front is " + queue.front());
		System.out.println("The rear is " + queue.rear());


10 is added to queue
20 is added to queue
30 is added to queue
10 removed from the queue
The front is 20
The rear is 30

Please leave a comment, if you want anything to be corrected or any suggestion on the topic.

Recommended: Stack Implementation in Java using an Array

Leave A Reply

Your email address will not be published.