File tree 3 files changed +74
-10
lines changed
main/java/com/crossoverjie/algorithm
test/java/com/crossoverjie
3 files changed +74
-10
lines changed Original file line number Diff line number Diff line change 9
9
* Date: 09/02/2018 23:51
10
10
* @since JDK 1.8
11
11
*/
12
- public class TwoStackQueue {
12
+ public class TwoStackQueue < T > {
13
13
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 () ;
16
23
17
24
18
25
/**
19
26
* 写入队列
20
- * @param object
27
+ * @param t
21
28
*/
22
- private void appendTail (Object object ){
23
- input .push (object ) ;
29
+ public void appendTail (T t ){
30
+ input .push (t ) ;
24
31
}
25
32
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
+ }
26
54
27
55
}
Original file line number Diff line number Diff line change @@ -15,10 +15,10 @@ public void test(){
15
15
map .put (2 ,2 ) ;
16
16
17
17
Object o = map .get (1 );
18
- LOGGER .info ("size ={}" ,map .size ());
18
+ LOGGER .info ("getSize ={}" ,map .size ());
19
19
20
20
map .remove (1 ) ;
21
- LOGGER .info ("size " +map .size ());
21
+ LOGGER .info ("getSize " +map .size ());
22
22
}
23
23
24
24
public static void main (String [] args ) {
@@ -27,10 +27,10 @@ public static void main(String[] args) {
27
27
map .put (2 ,2 ) ;
28
28
29
29
Object o = map .get (1 );
30
- LOGGER .info ("size ={}" ,map .size ());
30
+ LOGGER .info ("getSize ={}" ,map .size ());
31
31
32
32
map .remove (1 ) ;
33
33
map .remove (2 ) ;
34
- LOGGER .info ("size " +map .size ());
34
+ LOGGER .info ("getSize " +map .size ());
35
35
}
36
36
}
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