You are viewing a single comment's thread. Return to all comments →
Java implementation.
class MyQueue<T>{ Stack<T> a; Stack<T> b; MyQueue(){ a = new Stack<T>(); b = new Stack<T>(); } protected void enqueue(T t){ a.push(t); } protected void dequeue(){ if(b.isEmpty()){ while(!a.isEmpty()){ b.push(a.pop()); } } b.pop(); } protected String peek(){ if(b.isEmpty()){ while(!a.isEmpty()){ b.push(a.pop()); } } return b.peek().toString(); } }
Queues: A Tale of Two Stacks
You are viewing a single comment's thread. Return to all comments →
Java implementation.