題目:用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。
經典題,不多說,直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push( int node) { stack1.push(node); } public int pop() { while (!stack1.isEmpty()){ stack2.push(stack1.pop()); } int pop = stack2.pop(); while (!stack2.isEmpty()){ stack1.push(stack2.pop()); } return pop; } } |
總結
以上就是本文關于Java編程用兩個棧實現隊列代碼分享的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指正,小編會及時回復大家并修改,給廣大編程愛好者提供更好的閱讀體驗和幫助,感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/diu_brother/article/details/52558144