Created
August 26, 2016 02:51
-
-
Save ramseyboy/e893b7d53d15d7014190eaf15b8347a3 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
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