Queue Using Array Implementation In JAVA

Queue  Using Array Implementation In JAVA

import java.io.*;
class queue
{
int q[];
int front;
int rear;
int temp;
int max;
queue(int n)
{
max=n;
q=new int[max];
front=0;
rear=-1;
}
void push(int a)
{
int b;
b=a;
if(rear>=max-1)
{
System.out.println(“tt*****queue  is overflow!!!!!******n”);
}
else
{
rear=rear+1;
q[rear]=b;
 
}
}
void pop()
{
if(rear<front)
{
System.out.println(“ttstack is underflow!!!!!n”);
}
else
{
System.out.println(“”+q[front]+”is deletedn”);
front=front+1;
 
}
}
void display()
{
System.out.println(“nttElements are:”);
for(int i=front;i<=rear;i++)
{
System.out.println(“”+”tt”+q[i]);
 
}System.out.println(“n”);
}
}
class q1
{
static int ch;
 
public static void main(String args[])
{
 
 
queue obj=new queue(5);
 
do
{
try
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
 
System.out.println(“1:push”);
 
System.out.println(“2:pop”);
 
System.out.println(“3:display”);
System.out.println(“4:exitn”);
System.out.println(“ttenter ur choicett”);
ch=Integer.parseInt(b.readLine());
switch(ch)
 
{
case 1:System.out.println(“enterthe element to  be inserted=”);
 
int c=Integer.parseInt(b.readLine());
obj.push(c);
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
case 4:
System.out.println(“exit”);
break;
default:
System.out.println(“invalid choicen”);
}}
catch(Exception e)
{
System.out.println(e);
}
}
while(ch!=4);
}
}