Skip to content

Commit 5a32644

Browse files
committed
💡 Documenting source code.
1 parent deeee57 commit 5a32644

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

src/main/java/com/crossoverjie/algorithm/TwoStackQueue.java

+34-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,47 @@
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
}

src/test/java/com/crossoverjie/actual/AbstractMapTest.java

+4-4
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)