Created
May 10, 2020 22:31
-
-
Save TheGloriousGenesis/e7495a737c6d8dff141369c45aae457d to your computer and use it in GitHub Desktop.
Queue with two stacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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