Last active
February 22, 2019 08:46
-
-
Save mikeyang01/bc0e466c073297871dfe96a87204df46 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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