/*
* DESCRIPTION:
* ----------------------
* Queue implementation in Java using array.
*
* VARIABLES USE:
* ------------------------------
* int HEAD-This pointer(not a 'C' pointer!) deals with dequeue process.
* int TAIL-This pointer deals with enqueue process.
* int queue-The actual Queue datastructure in array of int type.
*
* METHODS USE:
*------------------------
*public void enqueue(int temp) - To enqueue an value(int type).
*public void dequeue() - To dequeue the first inserted element.
*public void display()-display the queue contents.
*
*NOTE:
*----------
*Empty elements in Queue is represented as '0'(zero).We cannot make it as 'null'.
*So,the queue initially contains only '0'
*/
import java.util.*;
public class queue_array
{
int HEAD=0; //the head of queue
int TAIL=0; //the tail of queue.
//THUS,BOTH TAIL AND HEAD POINTS THE SAME LOCATION INITIALLY
int[] queue=null; //Our Queue
queue_array(int size) //initializing size
{
queue=new int[size];
for(int i=0;i<=queue.length-1;i++)//assigning "0" value
{
queue[i]=0;
}
}
public void enqueue(int temp)
{
//Enqueue process can be done until TAIL finds a non-zero array element
if(queue[TAIL]==0)
{
queue[TAIL]=temp;//the given value is enqueued
TAIL=(TAIL+1)%(queue.length);//moving the TAIL to next location
//Here ,% operator is used to prevent TAIL pointer from moving beyond queue length
//If queue size=5,then possible values of TAIL are {0,1,2,3,4}.Calculate and see!
System.out.println("Element "+temp+" added to the Queue.\nTAIL="+TAIL);//just a confirmation message
}
else
{
System.out.println("Queue is full!\n");//if queue[TAIL] contains non-zero elements,then the queue is full
}
}
public void dequeue()
{
//dequeue process can be done until HEAD finds a empty array('0' in our case)
if(queue[HEAD]!=0)
{
int temp=queue[HEAD];//getting the array element before dequeueing it
queue[HEAD]=0;//setting the value to '0'('0' means the element is removed/ null in our case)
HEAD=(HEAD+1)%(queue.length);
System.out.println("Element "+temp+" removed from the Queue.\n");//just a confirmation message
}
else
{
System.out.println("Queue is empty");//if '0' is found ,then the queue is already empty.Deque is not valid
}
}
public void display()
{
/*To display the queue contents.
This visual representation is based on actual physical memory where our PC will store in this way.So
the HEAD wont point to starting array element and TAIL wont point to last array element.
So,think Queue in "circular form" to make it sense to us.
Since we do several enqueue and dequeue operations ,both the pointers move to different location.
But they still(always) follows FIFO"first in first out" operation.
Track the first element you enqueued(after serveral enqueue and queue operations).You will see it will be dequeued first even if it is inbetween the array("in actual physical rep")
So,dont get confused by the print statement representation.
*/
for(int i=0;i<=queue.length-1;i++)
{
System.out.print(queue[i]+"|");
}
//System.out.println("_________________________");
}
public static void main(String args[])
{
int option=0;
Scanner ip=new Scanner(System.in);//to get input from user
System.out.println("Enter the size of the Queue(any non zero):");
int size=ip.nextInt();
queue_array sample=new queue_array(size);
do{
System.out.println("\n\nQueue\n=========");
System.out.println("1.Enqueue an element.\n");
System.out.println("2.Dequeue an element.\n");
System.out.println("3.Display Queue contents.\n");
System.out.println("4.Exit.\n Enter a Choice:");
option=ip.nextInt();
switch(option)
{
case 1:
System.out.println("Enter an element to Enqueue into:");
sample.enqueue(ip.nextInt());
break;
case 2:
sample.dequeue();
break;
case 3:
sample.display();
break;
case 4:
System.exit(0);
default:
System.out.println("ENTER A VALUE BETWEEN 1 TO 4");
break;
}//switch
}while(option!=4);
}//main
}//class