Tag Archives: Circular Queue

Java Data Structure Implementation : Queue (Circular) using Array

This program demonstrates the Queue (Circular in nature) implementation using Array in Java. The Insert(Enqueue), Remove(Dequeue), Print Elements Operation has been implemented.

Find the Java Code below :

import java.util.Scanner;

/*
* This class demonstrates Queue (Circular in nature) Implementation using Array in java with Insert/Delete/Print Operations.
* @author : Madan Gopal Singh
* @version : 3.0
* @date : 11th April, 2014
*/

public class Queue {
public static int front = -1;
public static int rear = -1;
public static int QUEUE_SIZE = 5;
public static Object[] queueList;
public Queue() {}

/**
* This method insert the element into the queue from rear end.
* @param object.
* @return void.
*/
public static void insertIntoQueue(Object obj){
System.out.println("rear value is : "+rear);
if(rear == QUEUE_SIZE-1){
System.out.println("Queue is overflow");
}else{
rear = rear+1;
if(rear == 0)
front =0;
queueList[rear] = obj;
System.out.println("Element Inserted : "+queueList[rear].toString());
}
}

/**
* This method remove the element from the queue from front end.
* @param void.
* @return Object.
*/

public static Object removeFromQueue(){
System.out.println("Rear Value at time of deletion is : "+rear);
if(rear == -1 || front == -1){
System.out.println("Queue is underflow");
return null;
}else{
Object deletedElement = queueList[front];
System.out.println("Element removed : "+queueList[front]);
if(front==rear){
front = -1;
rear = rear-1;
}else{
for(int i =front;i<=rear-1;i++){
System.out.println("Shifting element in "+i+"("+queueList[i]+") from "+(i+1)+"("+queueList[i+1]+")");
queueList[i]=queueList[i+1];
}
rear = rear-1;
}
return deletedElement;
}
}

/**
* This method print all elements of the queue.
* @param void.
* @return void.
*/
public static void printAllElements(){
if(rear==-1){
System.out.println("No Element in Queue to display");
}else{
for(int i=0;i<=rear;i++){
System.out.println("Element at position "+i+" is : "+queueList[i].toString());
}
}
}

/**
* This method to check whether element already exists or not.
* @param Object.
* @return boolean.
*/
public static boolean isElementExists(Object obj){
if(rear==-1){
return false;
}else{
for(int i=0;i<=rear;i++){
if(queueList[i].toString().equalsIgnoreCase((String)obj.toString())){
return true;
}
}
return false;
}
}

/**
* This method to check whether queue is empty or not.
* @return boolean.
*/
public static boolean isEmpty(){
if(rear==-1){
return true;
}else{
return false;
}
}

/**
* Main Method
* @param args command line argument list
* @return void.
*/
public static void main(String[] args) {
queueList = new Object[QUEUE_SIZE];

int userOperation=0;
String mainMenuString = "";
Scanner sc = new Scanner(System.in);
do{
mainMenuString = "What operation you want to perform?"+
"\n 1. Insert Element "+
"\n 2. Delete Element "+
"\n 3. Print All Elements "+
"\n 4. Element Exists Or Not "+
"\n -1. Exit from Menu"+
"\n Please enter Operation Number :";
System.out.println(mainMenuString);
userOperation = sc.nextInt();
switch (userOperation) {
case 1:
System.out.println("Please enter element to insert into queue?");
String insertElement = sc.next();
Queue.insertIntoQueue(insertElement);
break;
case 2:
Queue.removeFromQueue();
break;
case 3:
Queue.printAllElements();
break;
case 4:
System.out.println("Please enter element to check Existency?");
boolean isExists = Queue.isElementExists(sc.next());
if(isExists){
System.out.println("Exists");
}else{
System.out.println("Not Exists");
}
break;
case -1:
System.out.println("Exit Application");
System.exit(0);
default:
System.out.println("Please enter a valid operation number");
}
}while(userOperation!=-1);
}

}

Any suggestion is invited if any.