Can we use collections in this?
i found it way easier than the video code, because there i have to write stack operations again and again, but here inbuilt is given.
import java.util.*;
public class Main
{
public static void main(String[] args) {
popefficient p= new popefficient();
for(int i=1;i<6;i++)
{
p.push(i*10);
}
p.display();
System.out.println();
System.out.println(p.pop());
System.out.println(p.top());
p.display();
}
}
class popefficient
{
Queue<Integer> q1= new LinkedList<>();
Queue<Integer> q2= new LinkedList<>();
public void push(int item)
{
while(!q1.isEmpty())
{
q2.add(q1.remove());
}
q1.add(item);
while(!q2.isEmpty())
{
q1.add(q2.remove());
}
}
public int pop()
{
int rv=q1.peek();
q1.remove(rv);
return rv;
}
public int top()
{
return q1.peek();
}
public void display()
{
Iterator itr= q1.iterator();
while(itr.hasNext())
{
System.out.print(itr.next()+" ");
}
}
}