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

java实现——007用两个栈实现队列
 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 }
java实现——007用两个栈实现队列

 

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

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

上一篇:spring配置hibernate的sessionFactory


下一篇:[LeetCode][Java]Candy@LeetCode