Skip to content

Commit bcec4f8

Browse files
committed
Ralf
1 parent e86803d commit bcec4f8

File tree

3 files changed

+277
-47
lines changed

3 files changed

+277
-47
lines changed

group20/925290009/第三次作业/数据结构练习/linkedlist/LinkedListTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package MyLinkedListTest;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import com.ralf.linkedlist.MyLinkedList;
10+
11+
public class MyLinkedListTest {
12+
13+
private MyLinkedList<Integer> list = new MyLinkedList<>();
14+
@Before
15+
public void setUp() throws Exception {
16+
17+
list.add(1);
18+
list.add(2);
19+
list.add(3);
20+
list.add(4);
21+
list.add(5);
22+
23+
}
24+
25+
@Test
26+
public void reverse() {
27+
28+
list.reverse();
29+
Assert.assertEquals(5, list.size());
30+
for(int i=0; i<list.size(); i++){
31+
Assert.assertEquals(5-i, list.get(i).intValue());
32+
}
33+
}
34+
35+
@Test
36+
public void removeFirstHalf(){
37+
//奇数个元素
38+
list.removeFirstHalf();
39+
Assert.assertEquals(3, list.size());
40+
41+
for(int i=0; i<list.size(); i++){
42+
Assert.assertEquals(i+3, list.get(i).intValue());
43+
}
44+
45+
//偶数个元素
46+
MyLinkedList<Integer> listA = new MyLinkedList<>();
47+
listA.add(2);
48+
listA.add(5);
49+
listA.add(7);
50+
listA.add(8);
51+
listA.removeFirstHalf();
52+
53+
Assert.assertEquals(2, listA.size());
54+
Assert.assertEquals(7, listA.get(0).intValue());
55+
Assert.assertEquals(8, listA.get(1).intValue());
56+
57+
}
58+
59+
@Test
60+
public void removeLastHalf(){
61+
62+
list.removeLastHalf();
63+
Assert.assertEquals(3, list.size());
64+
65+
for(int i=0; i<list.size(); i++){
66+
Assert.assertEquals(i+1, list.get(i).intValue());
67+
}
68+
69+
//偶数个元素
70+
MyLinkedList<Integer> listA = new MyLinkedList<>();
71+
listA.add(2);
72+
listA.add(5);
73+
listA.add(7);
74+
listA.add(8);
75+
listA.removeLastHalf();
76+
77+
Assert.assertEquals(2, listA.size());
78+
Assert.assertEquals(2, listA.get(0).intValue());
79+
Assert.assertEquals(5, listA.get(1).intValue());
80+
}
81+
82+
//remove(int i, int length)
83+
@Test
84+
public void remove(){
85+
86+
list.remove(0, 5);
87+
Assert.assertEquals(0, list.size());
88+
89+
MyLinkedList<Integer> listA = new MyLinkedList<>();
90+
listA.add(2);
91+
listA.add(5);
92+
listA.add(7);
93+
listA.add(8);
94+
listA.add(9);
95+
listA.add(10);
96+
97+
listA.remove(1, 3);
98+
Assert.assertEquals(3, listA.size());
99+
Assert.assertEquals(2, listA.get(0).intValue());
100+
Assert.assertEquals(9, listA.get(1).intValue());
101+
Assert.assertEquals(10, listA.get(2).intValue());
102+
}
103+
104+
105+
//int[] getElements(MyLinkedList<T> list)
106+
@Test
107+
public void getElements(){
108+
109+
MyLinkedList<Integer> listA = new MyLinkedList<>();
110+
listA.add(11);
111+
listA.add(101);
112+
listA.add(201);
113+
listA.add(301);
114+
listA.add(401);
115+
listA.add(501);
116+
listA.add(601);
117+
listA.add(701);
118+
119+
MyLinkedList<Integer> listB = new MyLinkedList<>();
120+
listB.add(1);
121+
listB.add(3);
122+
listB.add(4);
123+
listB.add(6);
124+
125+
int[] aar;
126+
aar = listA.getElements(listB);
127+
Assert.assertEquals(4, aar.length);
128+
129+
Assert.assertEquals(101, aar[0]);
130+
Assert.assertEquals(301, aar[1]);
131+
Assert.assertEquals(401, aar[2]);
132+
Assert.assertEquals(601, aar[3]);
133+
134+
}
135+
136+
137+
//subtract(MyLinkedList<T> list)
138+
@Test
139+
public void subtract(){
140+
141+
MyLinkedList<Integer> listA = new MyLinkedList<>();
142+
listA.add(11);
143+
listA.add(101);
144+
listA.add(201);
145+
listA.add(301);
146+
listA.add(401);
147+
listA.add(501);
148+
listA.add(601);
149+
listA.add(701);
150+
151+
MyLinkedList<Integer> listB = new MyLinkedList<>();
152+
listB.add(201);
153+
listB.add(301);
154+
listB.add(501);
155+
listB.add(801);
156+
157+
listA.subtract(listB);
158+
Assert.assertEquals(5, listA.size());
159+
160+
Assert.assertEquals(11, listA.get(0).intValue());
161+
Assert.assertEquals(101, listA.get(1).intValue());
162+
Assert.assertEquals(401, listA.get(2).intValue());
163+
Assert.assertEquals(601, listA.get(3).intValue());
164+
Assert.assertEquals(701, listA.get(4).intValue());
165+
}
166+
167+
@Test
168+
public void removeRepeatValues(){
169+
//自己的方法,但是是无序的,删除时是删除前面的相同元素
170+
MyLinkedList<Integer> listA = new MyLinkedList<>();
171+
listA.add(11);//
172+
listA.add(101);//
173+
listA.add(101);
174+
listA.add(301);//
175+
listA.add(11);
176+
listA.add(301);
177+
listA.add(201);//
178+
listA.add(701);//
179+
180+
listA.removeRepeatValues();
181+
Assert.assertEquals(5, listA.size());
182+
183+
Assert.assertEquals(101, listA.get(0).intValue());
184+
Assert.assertEquals(11, listA.get(1).intValue());
185+
Assert.assertEquals(301, listA.get(2).intValue());
186+
Assert.assertEquals(201, listA.get(3).intValue());
187+
Assert.assertEquals(701, listA.get(4).intValue());
188+
}
189+
190+
@Test
191+
public void removeDuplicateValues(){
192+
193+
MyLinkedList<Integer> listA = new MyLinkedList<>();
194+
195+
listA.add(11);
196+
listA.add(11);
197+
listA.add(12);
198+
listA.add(13);
199+
listA.add(14);
200+
listA.add(14);
201+
listA.add(15);
202+
listA.add(16);
203+
listA.add(16);
204+
205+
listA.removeDuplicateValues();
206+
Assert.assertEquals(6, listA.size());
207+
208+
Assert.assertEquals(11, listA.get(0).intValue());
209+
Assert.assertEquals(12, listA.get(1).intValue());
210+
Assert.assertEquals(13, listA.get(2).intValue());
211+
Assert.assertEquals(14, listA.get(3).intValue());
212+
Assert.assertEquals(15, listA.get(4).intValue());
213+
Assert.assertEquals(16, listA.get(5).intValue());
214+
215+
}
216+
217+
218+
//removeRange(int min, int max)
219+
@Test
220+
public void removeRange(){
221+
222+
MyLinkedList<Integer> listA = new MyLinkedList<>();
223+
224+
listA.add(11);
225+
listA.add(11);
226+
listA.add(12);
227+
listA.add(13);
228+
listA.add(14);
229+
listA.add(14);
230+
listA.add(15);
231+
listA.add(16);
232+
listA.add(16);
233+
234+
listA.removeRange(12, 16);
235+
236+
Assert.assertEquals(5, listA.size());
237+
Assert.assertEquals(11, listA.get(0).intValue());
238+
Assert.assertEquals(11, listA.get(1).intValue());
239+
Assert.assertEquals(12, listA.get(2).intValue());
240+
Assert.assertEquals(16, listA.get(3).intValue());
241+
Assert.assertEquals(16, listA.get(4).intValue());
242+
243+
}
244+
245+
@Test
246+
public void intersection(){
247+
248+
MyLinkedList<Integer> list = new MyLinkedList<>();
249+
MyLinkedList<Integer> listB = new MyLinkedList<>();
250+
MyLinkedList<Integer> listC = new MyLinkedList<>();
251+
252+
list.add(11);
253+
list.add(12);
254+
list.add(13);
255+
list.add(14);
256+
list.add(15);
257+
list.add(17);
258+
list.add(18);
259+
260+
listB.add(10);
261+
listB.add(12);
262+
listB.add(14);
263+
listB.add(15);
264+
listB.add(18);
265+
266+
267+
listC = (MyLinkedList<Integer>) list.intersection(listB);
268+
269+
Assert.assertEquals(4, listC.size());
270+
Assert.assertEquals(12, listC.get(0).intValue());
271+
Assert.assertEquals(14, listC.get(1).intValue());
272+
Assert.assertEquals(15, listC.get(2).intValue());
273+
Assert.assertEquals(18, listC.get(3).intValue());
274+
}
275+
276+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
csdn����ˣ��Ժ����
1+
csdn在审核中,稍后补上

0 commit comments

Comments
 (0)