You are viewing a single comment's thread. Return to all comments →
I used a similar approach:
public static class MyQueue<T> { Stack<T> stackNewestOnTop = new Stack<T>(); Stack<T> stackOldestOnTop = new Stack<T>(); public void enqueue(T value) { if(stackNewestOnTop.isEmpty()) { stackNewestOnTop.push(value); return; } stackOldestOnTop.push(value); } public T peek() { return stackNewestOnTop.peek(); } public T dequeue() { T item = stackNewestOnTop.pop(); if(stackNewestOnTop.isEmpty()){ while(!stackOldestOnTop.isEmpty()){ stackNewestOnTop.push(stackOldestOnTop.pop()); } } return item; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Queues: A Tale of Two Stacks
You are viewing a single comment's thread. Return to all comments →
I used a similar approach: