Skip to content

Instantly share code, notes, and snippets.

@ramseyboy
Created August 26, 2016 02:51
Show Gist options
  • Save ramseyboy/e893b7d53d15d7014190eaf15b8347a3 to your computer and use it in GitHub Desktop.
Save ramseyboy/e893b7d53d15d7014190eaf15b8347a3 to your computer and use it in GitHub Desktop.
import java.util.ArrayDeque;
import java.util.Deque;
public class StackQueue<T> {
private Deque<T> in;
private Deque<T> out;
public StackQueue() {
this.in = new ArrayDeque<>();
this.out = new ArrayDeque<>();
}
public void enqueue(T item) {
in.push(item);
}
public T dequeue() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.pop();
}
public boolean isEmpty() {
return in.isEmpty() && out.isEmpty();
}
public int size() {
return in.size() + out.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment