Skip to content

Instantly share code, notes, and snippets.

@mikeyang01
Last active February 22, 2019 08:46
Show Gist options
  • Select an option

  • Save mikeyang01/bc0e466c073297871dfe96a87204df46 to your computer and use it in GitHub Desktop.

Select an option

Save mikeyang01/bc0e466c073297871dfe96a87204df46 to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class Queue_TwoStack {
private static Stack<Object> stack1 = new Stack<>();
private static Stack<Object> stack2 = new Stack<>();
public static void enqueue(Object item) {
stack1.push(item);
}
public static Object dequeue() {
while (!stack1.empty())
stack2.push(stack1.pop());
if (!stack2.empty())
return stack2.pop();
else
return null;
}
public static Object getFront() {
while (!stack1.empty())
stack2.push(stack1.pop());
if (!stack2.empty())
return stack2.peek();
else
return null;
}
public static void main(String[] args) {
int[] a = {1, 2, 3};
for (int i = 0; i < a.length; i++) {
enqueue(a[i]);
System.out.println(a[i]+" ");
}
System.out.println("dequeue:" + dequeue());
System.out.println("getFront:" + getFront());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment