Skip to content

Commit 0a27f55

Browse files
authored
Merge pull request #3 from zavier/master
sync
2 parents dcd38a8 + e7101d7 commit 0a27f55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2416
-7
lines changed

group01/349209948/src/week1_0226/LinkedList.java

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,213 @@ private static class Node{
126126
next = null;
127127
}
128128
}
129+
/********************************************************************************************************************************************
130+
*
131+
* 第二次作业
132+
*
133+
*
134+
*
135+
*/
136+
/**
137+
* 把该链表逆置
138+
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
139+
*/
140+
public void reverse(){
141+
Node reversedNode = null;
142+
while (head != null) {
143+
Node temp = head;
144+
head = head.next;
145+
temp.next = reversedNode;
146+
reversedNode = temp;
147+
}
148+
head = reversedNode;
149+
}
150+
151+
/**
152+
* 删除一个单链表的前半部分
153+
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
154+
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10
155+
156+
*/
157+
public void removeFirstHalf(){
158+
int newStartIndex = size/2;
159+
for (int i =0;i<newStartIndex;i++){
160+
head = head.next;
161+
}
162+
size = size - newStartIndex;
163+
}
164+
165+
/**
166+
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
167+
* @param i
168+
* @param length
169+
*/
170+
public void remove(int i, int length){
171+
if (i< 0) {
172+
throw new IllegalArgumentException();
173+
}
174+
if (i +length > size) {
175+
length = size -i;
176+
}
177+
if (i == 0) {
178+
for (int j = 0; j< length; j ++) {
179+
head = head.next;
180+
}
181+
} else {
182+
Node startNode = (Node) this.get(i);
183+
Node endNode = (Node) this.get(i + length);
184+
startNode.next = endNode;
185+
}
186+
size = size - length;
187+
188+
189+
}
190+
/**
191+
* 假定当前链表和list均包含已升序排列的整数
192+
* 从当前链表中取出那些list所指定的元素
193+
* 例如当前链表 = 11->101->201->301->401->501->601->701
194+
* listB = 1->3->4->6
195+
* 返回的结果应该是[101,301,401,601]
196+
* @param list
197+
*/
198+
public int[] getElements(LinkedList list){
199+
checkList(list);
200+
int[] dest = new int[list.size];
201+
int arrayIndex = 0;
202+
while (list.head != null) {
203+
dest[arrayIndex] = (int)this.get((int)list.head.data);
204+
++ arrayIndex;
205+
list.head = list.head.next;
206+
}
207+
return dest;
208+
}
209+
210+
private void checkList(LinkedList list) {
211+
for (int i = 0; i <list.size; i++){
212+
rangeCheck((int)list.get(i));
213+
}
214+
215+
}
216+
/**
217+
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
218+
* 从当前链表中中删除在list中出现的元素
219+
220+
* @param list
221+
*/
222+
223+
public void subtract(LinkedList list){
224+
if (list == null || list.size == 0 || this.size == 0) {
225+
return;
226+
}
227+
int index = 0;
228+
int listIndex = 0;
229+
Node temp = head;
230+
while (true) {
231+
if((int)temp.data <= (int)list.get(listIndex)){
232+
index ++;
233+
temp = temp.next;
234+
} else if ((int)temp.data == (int)list.get(listIndex)) {
235+
this.remove(index);
236+
index ++;
237+
listIndex ++;
238+
} else if (index >= this.size || listIndex >= list.size) {
239+
break;
240+
}
241+
}
242+
}
243+
244+
/**
245+
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
246+
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
247+
*/
248+
public void removeDuplicateValues(){
249+
if (this.size == 0) {
250+
return;
251+
}
252+
Node subHead = head;
253+
Node subTail = head;
254+
while(true) {
255+
if (subTail == null) {
256+
subHead.next = null;
257+
break;
258+
}
259+
if ((int)subTail.data == (int)subHead.data) {
260+
if (!(subTail ==subHead)){
261+
this.size --;
262+
}
263+
subTail = subTail.next;
264+
} else {
265+
subHead.next = subTail;
266+
subHead = subHead.next;
267+
}
268+
}
269+
}
270+
271+
/**
272+
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
273+
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
274+
* @param min
275+
* @param max
276+
*/
277+
//一地bug。。。
278+
public void removeRange(int min, int max){
279+
if (this.size ==0) {
280+
return;
281+
}
282+
if ((int)head.data > max) {
283+
throw new IllegalArgumentException();
284+
}
285+
Node temp = head;
286+
int index = 0;
287+
int from = 0;
288+
int to = 0;
289+
while (temp != null) {
290+
if ((int)temp.data <= min) {
291+
temp = temp.next;
292+
} else if ((int)temp.data > min && from == 0){
293+
from = index;
294+
}
295+
if ((int)temp.data < max && from > 0) {
296+
temp = temp.next;
297+
} else if((int)temp.data > max && to == 0) {
298+
to = index;
299+
}
300+
++ index;
301+
}
302+
if (to == 0) {
303+
this.remove(from, this.size - from);
304+
} else {
305+
this.remove(from, to - from);
306+
}
307+
}
308+
309+
/**
310+
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
311+
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
312+
* @param list
313+
*/
314+
public LinkedList intersection( LinkedList list){
315+
if (this.size ==0 || list.size == 0){
316+
return null;
317+
}
318+
Node tempHead = head;
319+
int listIndex = 0;
320+
LinkedList newList = new LinkedList();
321+
while (true) {
322+
if(tempHead == null || list.size < listIndex) {
323+
break;
324+
}
325+
if ((int)tempHead.data < (int)list.get(listIndex)){
326+
tempHead = head.next;
327+
} else if ((int)tempHead.data > (int)list.get(listIndex)){
328+
listIndex ++;
329+
} else if ((int)tempHead.data == (int)list.get(listIndex)){
330+
newList.add(tempHead);
331+
tempHead = tempHead.next;
332+
++ listIndex;
333+
}
334+
}
335+
336+
return newList;
337+
}
129338
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package week3_0312;
2+
3+
import java.io.IOException;
4+
import java.io.RandomAccessFile;
5+
6+
import week3_0312.api.Connection;
7+
8+
public class DownloadThread extends Thread{
9+
10+
Connection conn;
11+
int startPos;
12+
int endPos;
13+
14+
public DownloadThread( Connection conn, int startPos, int endPos){
15+
16+
this.conn = conn;
17+
this.startPos = startPos;
18+
this.endPos = endPos;
19+
}
20+
public void run(){
21+
byte[] data = new byte[endPos - startPos];
22+
try {
23+
data = conn.read(startPos, endPos);
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
writeToFile(data);
28+
}
29+
public void writeToFile(byte[] data) {
30+
RandomAccessFile file;
31+
try {
32+
file = new RandomAccessFile("downloadTest.jpg","rw");
33+
file.seek(startPos);
34+
file.write(data, 0, data.length);
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package week3_0312;
2+
3+
import java.io.IOException;
4+
5+
import week3_0312.api.Connection;
6+
import week3_0312.api.ConnectionException;
7+
import week3_0312.api.ConnectionManager;
8+
import week3_0312.api.DownloadListener;
9+
10+
11+
public class FileDownloader {
12+
13+
String url;
14+
15+
DownloadListener listener;
16+
17+
ConnectionManager cm;
18+
19+
20+
public FileDownloader(String _url) {
21+
this.url = _url;
22+
23+
}
24+
25+
public void execute(){
26+
// 在这里实现你的代码, 注意: 需要用多线程实现下载
27+
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
28+
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
29+
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
30+
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
31+
// 具体的实现思路:
32+
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
33+
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
34+
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
35+
// 3. 把byte数组写入到文件中
36+
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
37+
38+
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
39+
Connection conn = null;
40+
try {
41+
42+
conn = cm.open(this.url);
43+
44+
int length = conn.getContentLength();
45+
DownloadThread downloadThread1 = new DownloadThread(conn,0,length/3);
46+
downloadThread1.start();
47+
DownloadThread downloadThread2 = new DownloadThread(conn, length/3+1, length/3 *2);
48+
downloadThread2.start();
49+
DownloadThread downloadThread3 = new DownloadThread(conn, length/3*2 +1, length -1);
50+
downloadThread3.start();
51+
try {
52+
downloadThread1.join();
53+
downloadThread2.join();
54+
downloadThread3.join();
55+
listener.notifyFinished();
56+
} catch(InterruptedException e) {
57+
e.printStackTrace();
58+
}
59+
60+
} catch (ConnectionException e) {
61+
e.printStackTrace();
62+
}finally{
63+
if(conn != null){
64+
conn.close();
65+
}
66+
}
67+
68+
69+
70+
71+
}
72+
73+
public void setListener(DownloadListener listener) {
74+
this.listener = listener;
75+
}
76+
77+
78+
79+
public void setConnectionManager(ConnectionManager ucm){
80+
this.cm = ucm;
81+
}
82+
83+
public DownloadListener getListener(){
84+
return this.listener;
85+
}
86+
87+
}

0 commit comments

Comments
 (0)