Java Dynamic Queue Using Array

Chapter: Data Structures Last Updated: 26-09-2018 03:34:58 UTC

Program:

            /* ............... START ............... */
                
public class JavaDynamicQueue {

	private int capacity = 2;
	int queueArr[];
	int front = 0;
	int rear = -1;
	int currentSize = 0;

	public JavaDynamicQueue() {
		queueArr = new int[this.capacity];
	}

	public void enqueue(int item) {

		if (isQueueFull()) {
			System.out.println("Queue is full, increase capacity...");
			increaseCapacity();
		}
		rear++;
		if (rear >= queueArr.length && currentSize != queueArr.length) {
			rear = 0;
		}
		queueArr[rear] = item;
		currentSize++;
		System.out.println("Adding: " + item);
	}

	public void dequeue() {
		if (isQueueEmpty()) {
			System.out.println("Underflow ! Unable to remove element from Queue");
		} else {
			front++;
			if (front > queueArr.length - 1) {
				System.out.println("removed: " + queueArr[front - 1]);
				front = 0;
			} else {
				System.out.println("removed: " + queueArr[front - 1]);
			}
			currentSize--;
		}
	}

	public boolean isQueueFull() {
		boolean status = false;
		if (currentSize == queueArr.length) {
			status = true;
		}
		return status;
	}

	public boolean isQueueEmpty() {
		boolean status = false;
		if (currentSize == 0) {
			status = true;
		}
		return status;
	}

	private void increaseCapacity() {

		int newCapacity = this.queueArr.length * 2;
		int[] newArr = new int[newCapacity];
		int tmpFront = front;
		int index = -1;
		while (true) {
			newArr[++index] = this.queueArr[tmpFront];
			tmpFront++;
			if (tmpFront == this.queueArr.length) {
				tmpFront = 0;
			}
			if (currentSize == index + 1) {
				break;
			}
		}

		this.queueArr = newArr;
		System.out.println("New array capacity: " + this.queueArr.length);

		this.front = 0;
		this.rear = index;
	}

	public static void main(String a[]) {

		JavaDynamicQueue queue = new JavaDynamicQueue();
		queue.enqueue(4);
		queue.dequeue();
		queue.enqueue(56);
		queue.enqueue(2);
		queue.enqueue(67);
		queue.dequeue();
		queue.enqueue(24);
		queue.enqueue(98);
		queue.dequeue();
		queue.dequeue();
		queue.dequeue();
		queue.enqueue(435);
		queue.dequeue();
		queue.dequeue();
	}
}
                /* ............... END ............... */
        

Output

Adding: 4
removed: 4
Adding: 56
Adding: 2
Queue is full, increase capacity...
New array capacity: 4
Adding: 67
removed: 56
Adding: 24
Adding: 98
removed: 2
removed: 67
removed: 24
Adding: 435
removed: 98
removed: 435

Notes:

  • A queue is a kind of abstract data type or collection in which the entities in the collection are kept in order and the only operations on the collection are the addition of entities to the rear terminal position, called as enqueue, and removal of entities from the front terminal position, called as dequeue.

Tags

Dynamic Queue Using Array, Java, Data Structures, queue implementation in java using array, queue and dequeue implementation in java, array queue java, dynamic implementation of queue in data structure, java queue example

Similar Programs Chapter Last Updated
HashMap Implementation In Java Data Structures 07-07-2018
Linked List Implementation In Java Data Structures 09-03-2018
Queue Implementation In Java Data Structures 22-09-2018
Stack Implementation In Java Data Structures 22-09-2018
Binary Search Tree Java Data Structures 11-02-2018
Insertion Sort In Java Data Structures 10-02-2018
Java Stack Example Data Structures 16-10-2017
Java String Array Sort Data Structures 31-03-2017
Java Hashmap Add Key And Value Data Structures 25-03-2017
Java Binary Tree Vertical Sum Data Structures 11-11-2016
Java Binary Tree Boundary Traversal Data Structures 11-11-2016
Java Binary Tree Lowest Common Ancestor (LCA) Data Structures 11-11-2016
Java Binary Tree Maximum Element Data Structures 11-11-2016
Java Three Dimensional Array Data Structures 28-10-2016
Java Infix Expression To Postfix Expression Data Structures 25-10-2016
Java Linked List Add Element First And Last Data Structures 24-10-2016
Java Maximum Element From Vector Data Structures 24-10-2016
Java Binary Search On Vector Data Structures 24-10-2016
Java Get Elements Of LinkedList Data Structures 23-10-2016
Java Linked List Update Element Data Structures 23-10-2016
Java Delete Elements From LinkedList Data Structures 23-10-2016
Java Double Ended Queue Data Structures 09-10-2016
Java Queue Array Based Implementation Data Structures 07-10-2016
Java Sort Short Array Data Structures 25-09-2016
Java Sort Long Array Data Structures 25-09-2016
Java Sort Int Array Data Structures 19-09-2016
Java Sort Float Array Data Structures 19-09-2016
Java Sort Double Array Data Structures 19-09-2016
Java Sort Char Array Data Structures 19-09-2016
Java Sort Byte Array Data Structures 19-09-2016

1 2 3