File tree Expand file tree Collapse file tree
main/java/com/crossoverjie/algorithm
test/java/com/crossoverjie Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99 * Date: 09/02/2018 23:51
1010 * @since JDK 1.8
1111 */
12- public class TwoStackQueue {
12+ public class TwoStackQueue < T > {
1313
14- private Stack input = new Stack () ;
15- private Stack out = new Stack () ;
14+ /**
15+ * 写入的栈
16+ */
17+ private Stack <T > input = new Stack () ;
18+
19+ /**
20+ * 移除队列所出的栈
21+ */
22+ private Stack <T > out = new Stack () ;
1623
1724
1825 /**
1926 * 写入队列
20- * @param object
27+ * @param t
2128 */
22- private void appendTail (Object object ){
23- input .push (object ) ;
29+ public void appendTail (T t ){
30+ input .push (t ) ;
2431 }
2532
33+ /**
34+ * 删除队列头结点 并返回删除数据
35+ * @return
36+ */
37+ public T deleteHead (){
38+
39+ //是空的 需要将 input 出栈写入 out
40+ if (out .isEmpty ()){
41+ while (!input .isEmpty ()){
42+ out .push (input .pop ()) ;
43+ }
44+ }
45+
46+ //不为空时直接移除出栈就表示移除了头结点
47+ return out .pop () ;
48+ }
49+
50+
51+ public int getSize (){
52+ return input .size () + out .size () ;
53+ }
2654
2755}
Original file line number Diff line number Diff line change @@ -15,10 +15,10 @@ public void test(){
1515 map .put (2 ,2 ) ;
1616
1717 Object o = map .get (1 );
18- LOGGER .info ("size ={}" ,map .size ());
18+ LOGGER .info ("getSize ={}" ,map .size ());
1919
2020 map .remove (1 ) ;
21- LOGGER .info ("size " +map .size ());
21+ LOGGER .info ("getSize " +map .size ());
2222 }
2323
2424 public static void main (String [] args ) {
@@ -27,10 +27,10 @@ public static void main(String[] args) {
2727 map .put (2 ,2 ) ;
2828
2929 Object o = map .get (1 );
30- LOGGER .info ("size ={}" ,map .size ());
30+ LOGGER .info ("getSize ={}" ,map .size ());
3131
3232 map .remove (1 ) ;
3333 map .remove (2 ) ;
34- LOGGER .info ("size " +map .size ());
34+ LOGGER .info ("getSize " +map .size ());
3535 }
3636}
Original file line number Diff line number Diff line change 1+ package com .crossoverjie .algorithm ;
2+
3+ import org .junit .Test ;
4+ import org .slf4j .Logger ;
5+ import org .slf4j .LoggerFactory ;
6+
7+ public class TwoStackQueueTest {
8+ private final static Logger LOGGER = LoggerFactory .getLogger (TwoStackQueueTest .class );
9+ @ Test
10+ public void queue (){
11+ TwoStackQueue <String > twoStackQueue = new TwoStackQueue <String >() ;
12+ twoStackQueue .appendTail ("1" ) ;
13+ twoStackQueue .appendTail ("2" ) ;
14+ twoStackQueue .appendTail ("3" ) ;
15+ twoStackQueue .appendTail ("4" ) ;
16+ twoStackQueue .appendTail ("5" ) ;
17+
18+
19+ int size = twoStackQueue .getSize ();
20+
21+ for (int i = 0 ; i < size ; i ++){
22+ LOGGER .info (twoStackQueue .deleteHead ());
23+ }
24+
25+ LOGGER .info ("========第二次添加=========" );
26+
27+ twoStackQueue .appendTail ("6" ) ;
28+
29+ size = twoStackQueue .getSize ();
30+
31+ for (int i = 0 ; i < size ; i ++){
32+ LOGGER .info (twoStackQueue .deleteHead ());
33+ }
34+ }
35+
36+ }
You can’t perform that action at this time.
0 commit comments