Thursday, 11 August 2016

queue operation in java

import java.io.*;
class queue
{
final int max=3;
int a[]=new int[max];
int F=-1,R=-1;

void insert(int x)
{
if(R==max-1)
System.out.println("insertion is not possible");
else
{
a[R+1]=x;
R++;
}
}

void delete()
{
if(F==R)
System.out.println("deletion is not Possible because Queue Is empty");
else
{
F=F+1;
System.out.println("delete item is .."+a[F]);
}
}

void display()
{
if(F==R)
System.out.println("queue is empty");
else
{
for(int i=F+1;i<=R;i++)
System.out.println(a[i]+"\n");
}
}
}

class queueop
{
public static void main(String args[])throws IOException
{
int ch;int x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
queue q=new queue();
while(true)
{
System.out.println("\n1. Insertion");
System.out.println("2. Deletion");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the element to be inserted");
    x=Integer.parseInt(br.readLine());
    q.insert(x);
    break;
case 2:    q.delete();
    break;
case 3: q.display();
    break;
case 4:System.exit(0);
}
}
}
}
Post a Comment