首页 > 编程语言 > 详细

java实现——007用两个栈实现队列

时间:2014-05-08 15:32:37      阅读:417      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 import java.util.Stack;
 2 
 3 public class T007 {
 4     public static void main(String[] args) {
 5         Queue q = new Queue();
 6         q.stack1 = new Stack();
 7         q.stack2 = new Stack();
 8         q.appendTail(1);
 9         System.out.println(q.stack1.toString());
10         System.out.println(q.stack2.toString());
11         q.appendTail(2);
12         q.appendTail(3);
13         q.deleteHead();
14         System.out.println(q.stack1.toString());
15         System.out.println(q.stack2.toString());
16     }
17 
18     public static class Queue {
19         private Stack<Integer> stack1;
20         private Stack<Integer> stack2;
21 
22         public void appendTail(int e) {
23             stack1.push(e);
24         }
25 
26         public void deleteHead() {
27             if (stack2.size() <= 0) {
28                 while (stack1.size() > 0) {
29                     int data = stack1.peek();
30                     stack1.pop();
31                     stack2.push(data);
32                 }
33             }
34             if (stack2.size() == 0)
35                 System.out.println("queue is empty");
36             int head = stack2.peek();
37             stack2.pop();
38         }
39     }
40 }
bubuko.com,布布扣

 

java实现——007用两个栈实现队列,布布扣,bubuko.com

java实现——007用两个栈实现队列

原文:http://www.cnblogs.com/thehappyyouth/p/3715735.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!