Skip to content

Instantly share code, notes, and snippets.

@TheGloriousGenesis
Created May 10, 2020 22:31
Show Gist options
  • Save TheGloriousGenesis/e7495a737c6d8dff141369c45aae457d to your computer and use it in GitHub Desktop.
Save TheGloriousGenesis/e7495a737c6d8dff141369c45aae457d to your computer and use it in GitHub Desktop.
Queue with two stacks
import java.util.Stack;
public class QueueWithTwoStacks {
public Stack<Integer> temp = new Stack<>();
public Stack<Integer> mockQueue = new Stack<>();
public void enqueue(final Integer value) {
for (int i=1; i <= mockQueue.size(); i++) {
temp.add(mockQueue.pop());
}
temp.add(value);
}
public Integer dequeue() {
for (int i=1; i <= temp.size(); i++) {
mockQueue.add(temp.pop());
}
return mockQueue.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment