Skip to content

Instantly share code, notes, and snippets.

@kelly-us

kelly-us/MyQueue Secret

Created April 28, 2015 06:49
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 kelly-us/ca1854a0b3d928a2db46 to your computer and use it in GitHub Desktop.
Save kelly-us/ca1854a0b3d928a2db46 to your computer and use it in GitHub Desktop.
public class MyQueue<T> {
Stack<T> s1, s2;
public MyQueue(){
s1 = new Stack<T>();
s2 = new Stack<T>();
}
public void add(T val){
s1.push(val);
}
public int size(){
return s1.size() + s2.size();
}
public void shiftStacks(){
if(s2.isEmpty()){
while(!s1.isEmpty()){
s2.push(s1.pop());
}
}
}
public T remove(){
shiftStacks();
return s2.pop();
}
public T peek(){
shiftStacks();
return s2.peek();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment