import java.io.*;
class Aray
{
private int a[];
public int c=0,i,front,rear;
public Aray()
{
a=new int[5];
front=0;
rear=-1;
}
public void insert(int val)
{
if(c<=4)
{
rear=(rear+1)%5;
a[rear]=val;
c++;
}
else
System.out.println("Queue is full");
}
public int delete()
{
if(c==0)
{
System.out.println("Queue is Empty");
return 0;
}
else
{
int temp = a[front];
front=(front+1)%5;
c--;
return temp;
}
}
public void display()
{
if(c==0)
{
System.out.println("Queue is Empty");
}
else
{
System.out.println("Elements from front to rear");
int m=0;
for(i=front;m<c;i++,m++)
{
if(i>4)
i=i%5;
System.out.println(a[i]);
}
}
}
}
class QueueArray
{
public static void main(String[] args) throws IOException
{
Aray a = new Aray();
System.out.println("1INSERT\n2.DELETE\n3.DISPLAY");
while(true)
{
System.out.println("Enter the Key of the Operation");
int c=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
switch(c)
{
case 1:
System.out.println("Enter the Element");
int val=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
a.insert(val);
break;
case 2:
int temp=a.delete();
if(temp!=0)
System.out.println("Element deleted is [" + temp + "] ");
break;
case 3:
a.display();
break;
case 4:
System.exit(0);
default:
System.out.println("You have entered invalid Key.\n Try again");
}
}
}
}
No comments:
Post a Comment