Wednesday, April 2, 2014

Java program for queue

import java.util.*;
class Queue
{
private LinkedList l=new LinkedList();

public void insert(Object o)
{

l.add(o);

}
public int size()
{
 return l.size();
}
public Object remove()
{
try{
return l.remove();
}catch(NoSuchElementException n)
{
System.out.print("\nQueue is Empty  ");
}
return null;
}
public Object[] display()
{
Object[] a=new Object[l.size()];

for(int i=0;i<l.size();i++)
a[i]=l.get(i);

return a;
}

}
class test
{
public static void main(String[] args)
{
Queue q=new Queue();
Object[] a;


q.insert(new Integer(1));

q.insert(new Float(1.5));

q.insert("rajesh");

System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");

a=q.display();

for(int i=0;i<a.length;i++)
System.out.println(a[i]);

System.out.print("\n\n");

for(int j=0;j<3;j++)
System.out.println(q.remove());

System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");

System.out.println(q.remove());

a=q.display();
System.out.print("\n\n");
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

No comments:

Post a Comment