-
-
Save kelly-us/ca1854a0b3d928a2db46 to your computer and use it in GitHub Desktop.
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
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