class Queue
{
int size=5;
Object item[]=new Object[size];
int rare=0,front=0;
boolean empty()
{
if((front==size)||(front==rare))
{
System.out.println("Queue is empty....");
front=0;
return(true);
}
else
{
return(false);
}
}
boolean full()
{
if(rare==size)
{
System.out.println("Queue is Full..cannot insert element ");
return(true);
}
else
{
return(false);
}
}
void inQueue(Object o,Queue z)
{ if(z.full())
{
}
else
{
item[rare++]=o;
System.out.println(" value is inserted.");
}
}
Object deQueue(Queue z)
{
if(z.empty())
{
return(null);
}
else
{
return(item[front++]);
}
}
void display(Queue z)
{
if(z.empty())
{
}
else
{
System.out.println("values in Queue are:");
for(int i=front;i<rare;i++)
{
System.out.print(" "+item[i]);
}
System.out.println();
}
}
}
class Queues
{
public static void main(String args[ ])throws IOException
{
Queue ob=new Queue();
int c,a=1;
DataInputStream dis=new DataInputStream(System.in);
while(a==1)
{
System.out.println();
System.out.println("1:to enter value into Queue:");
System.out.println("2:to delete the value from Queue:");
System.out.println("3:display Queue:");
System.out.println("4:to check Queue is full :");
System.out.println("5:exit");
System.out.println();
System.out.print("Enter your choice:");
System.out.println();
c=Integer.parseInt(dis.readLine());
switch(c)
{
case 1:
System.out.println("Enter your value:");
int x=Integer.parseInt(dis.readLine());
ob.inQueue(new Integer(x),ob);
break;
case 2: Object z=ob.deQueue(ob);
if(z!=null)
{
System.out.println();
System.out.println("value deleted:"+z);
}
break;
case 3:
System.out.println();
ob.display(ob);
break;
case 4:
if(ob.full())
{
System.out.println();
}
else
{
System.out.println("Queue is not Full.... ");
}
break;
case 5:
x=0;
System.exit(1);
break;
}
}
}
}
No comments:
Post a Comment