Queue Implementation in Java using Array

Filed Under: Java

Queue is a special type of collection designed to hold elements before processing and order elements in a FIFO (first-in-first-out) manner. It’s part of java collections framework.

Queue Implementation in Java

queue implementation in java using array, queue java implementation program example code

Here I am trying to implement Queue using array and provides basic functionalities like enqueue(obj), dequeue() and isEmpty().


package com.journaldev.util;

//CONSTRUCTION: with no initializer
//
// ******************PUBLIC OPERATIONS*********************
// void enqueue( obj )      -- Insert obj
// Object dequeue( )      -- Return and remove least recent item
// boolean isEmpty( )     -- Return true if empty; else false


public class MyQueue {
	
	public static final int DEFAULT_SIZE=5;
	
	private Object data[];
	
	private int index;
	
	public MyQueue(){
		data=new Object[DEFAULT_SIZE];
	}
	
	public boolean isEmpty(){
		return index==0;
	}
	
	public void enqueue(Object obj) throws Exception{
		if(index==DEFAULT_SIZE-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;
		
	}
	
	public static void main(String[] args) throws Exception {
		
		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");
	}

}

The above class will work as a Queue but the size is fixed and it throws an exception when it’s full.

We can make the size dynamic like we did with the ArrayList implementation with Array. I am leaving that part to you, have a look at the ArrayList implementation and try to implement the increase in queue size at runtime.

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