Skip to content

Instantly share code, notes, and snippets.

@nealhu
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealhu/bdcb03625883ae62f023 to your computer and use it in GitHub Desktop.
Save nealhu/bdcb03625883ae62f023 to your computer and use it in GitHub Desktop.
CC_3_5
// Cracking the Coding Interview
// 3.5 Implement a MyQueue class which implements a queue using two stacks.
import java.util.Stack;
class MyQueue<T> {
// push stack
private Stack<T> s0;
// poll stack
private Stack<T> s1;
private int cur;
public MyQueue() {
cur = 0;
s0 = new Stack<T>();
s1 = new Stack<T>();
}
public void push(T element) {
s0.push(element);
}
public T poll() {
if (!s1.empty()) {
while(!s0.empty()) {
s1.push(s0.pop());
}
}
return s1.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment