Skip to content

Commit 3b471a5

Browse files
committed
队列实现MyQueue
1 parent 17454cc commit 3b471a5

File tree

2 files changed

+67
-3
lines changed
  • group07/562247675/homework/homework-0226/src

2 files changed

+67
-3
lines changed
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
package com.coding2017.group7.homework.c0226;
22

3+
import java.util.EmptyStackException;
4+
35
public class MyQueue {
46

7+
private MyLinkedList myLinkedList = new MyLinkedList();
8+
59
public void enQueue(Object o) {
10+
myLinkedList.add(0, o);
611
}
712

813
public Object deQueue() {
9-
return null;
14+
if (isEmpty()) {
15+
throw new EmptyQueueException();
16+
}
17+
return myLinkedList.removeLast();
1018
}
1119

1220
public boolean isEmpty() {
13-
return false;
21+
return myLinkedList.size() <= 0;
1422
}
1523

1624
public int size() {
17-
return -1;
25+
return myLinkedList.size();
26+
}
27+
28+
private static class EmptyQueueException extends EmptyStackException {
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return myLinkedList.toString();
1834
}
1935
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.coding2017.group7.homework.c0226;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.FixMethodOrder;
7+
import org.junit.Test;
8+
import org.junit.runners.MethodSorters;
9+
10+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
11+
public class MyQueueTest {
12+
13+
private MyQueue myQueue = new MyQueue();
14+
private final Object[] elements = {1, 2, 3};
15+
private final int mySize = elements.length;
16+
17+
@Before
18+
public void setUp() throws Exception {
19+
for (int i = 0; i < mySize; i++) {
20+
myQueue.enQueue(i + 1);
21+
}
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
for (int i = myQueue.size(); i > 0; i--) {
27+
myQueue.deQueue();
28+
}
29+
}
30+
31+
@Test
32+
public void enQueue() throws Exception {
33+
myQueue.enQueue(-1);
34+
Object o = 0;
35+
for (int i = myQueue.size(); i > 0; i--) {
36+
o = myQueue.deQueue();
37+
}
38+
Assert.assertTrue(o.equals(-1));
39+
}
40+
41+
@Test
42+
public void deQueue() throws Exception {
43+
myQueue.enQueue(-1);
44+
Object o = myQueue.deQueue();
45+
Assert.assertTrue(o.equals(elements[0]));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)