You are viewing a single comment's thread. Return to all comments →
What is the issue with doing enqueue and the other methods this way?
public void enqueue(T value) { // Push onto newest stack for(int i = 0; i < stackOldestOnTop.size(); i++){ stackNewestOnTop.push(stackOldestOnTop.pop()); } stackNewestOnTop.push(value); for(int j = 0; j < stackNewestOnTop.size(); j++){ stackOldestOnTop.push(stackNewestOnTop.pop()); } } public T peek() { if(!stackOldestOnTop.isEmpty())return stackOldestOnTop.peek(); return null; } public T dequeue() { if(!stackOldestOnTop.isEmpty())return stackOldestOnTop.pop(); return null; } }
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 →
What is the issue with doing enqueue and the other methods this way?