Implement a queue using two stacks
Anonymous
public class StackQueue { private Stack buffer; private Stack queue; public StackQueue() { buffer = new Stack(); queue = new Stack(); } public void add(T t) { buffer.push(t); } public T poll() { if (queue.isEmpty()) { while (!buffer.empty()) { queue.add(buffer.pop()); } } return queue.pop(); } }
Check out your Company Bowl for anonymous work chats.