Interesting, but intuitive question.
My Solution:-
//QueueWith2Stacks.java
import java.util.Stack;
public class QueueWith2Stacks {
Stack< Object> insertStack=new Stack< Object>();
Stack< Object> popStack=new Stack< Object>();
void enqueue(Object element){
insertStack.push(element);
}
Object dequeue(){
if(popStack.empty()&& insertStack.empty())
return(null);
if(popStack.empty())//This is the only interesting part about it.
while(!insertStack.empty())
popStack.push(insertStack.pop());
return(popStack.pop());
}
void display(){
System.out.println(popStack.toString()+insertStack.toString());
}
}
Tuesday, October 02, 2007
Subscribe to:
Post Comments (Atom)
4 comments:
This is really usefull. Well done, Thanks
Thanks, this was super helpful. :)
Good work.
Hope to learn programming tech. like you have.......Great help!
Post a Comment