File tree Expand file tree Collapse file tree 2 files changed +67
-3
lines changed
group07/562247675/homework/homework-0226/src
main/java/com/coding2017/group7/homework/c0226
test/java/com/coding2017/group7/homework/c0226 Expand file tree Collapse file tree 2 files changed +67
-3
lines changed Original file line number Diff line number Diff line change 1
1
package com .coding2017 .group7 .homework .c0226 ;
2
2
3
+ import java .util .EmptyStackException ;
4
+
3
5
public class MyQueue {
4
6
7
+ private MyLinkedList myLinkedList = new MyLinkedList ();
8
+
5
9
public void enQueue (Object o ) {
10
+ myLinkedList .add (0 , o );
6
11
}
7
12
8
13
public Object deQueue () {
9
- return null ;
14
+ if (isEmpty ()) {
15
+ throw new EmptyQueueException ();
16
+ }
17
+ return myLinkedList .removeLast ();
10
18
}
11
19
12
20
public boolean isEmpty () {
13
- return false ;
21
+ return myLinkedList . size () <= 0 ;
14
22
}
15
23
16
24
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 ();
18
34
}
19
35
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments