Skip to content

Commit

Permalink
Merge pull request #3 from zavier/master
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
Viscaria233 authored Mar 12, 2017
2 parents dcd38a8 + e7101d7 commit 0a27f55
Show file tree
Hide file tree
Showing 44 changed files with 2,416 additions and 7 deletions.
209 changes: 209 additions & 0 deletions group01/349209948/src/week1_0226/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,213 @@ private static class Node{
next = null;
}
}
/********************************************************************************************************************************************
*
* 第二次作业
*
*
*
*/
/**
* 把该链表逆置
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse(){
Node reversedNode = null;
while (head != null) {
Node temp = head;
head = head.next;
temp.next = reversedNode;
reversedNode = temp;
}
head = reversedNode;
}

/**
* 删除一个单链表的前半部分
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10
*/
public void removeFirstHalf(){
int newStartIndex = size/2;
for (int i =0;i<newStartIndex;i++){
head = head.next;
}
size = size - newStartIndex;
}

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
* @param i
* @param length
*/
public void remove(int i, int length){
if (i< 0) {
throw new IllegalArgumentException();
}
if (i +length > size) {
length = size -i;
}
if (i == 0) {
for (int j = 0; j< length; j ++) {
head = head.next;
}
} else {
Node startNode = (Node) this.get(i);
Node endNode = (Node) this.get(i + length);
startNode.next = endNode;
}
size = size - length;


}
/**
* 假定当前链表和list均包含已升序排列的整数
* 从当前链表中取出那些list所指定的元素
* 例如当前链表 = 11->101->201->301->401->501->601->701
* listB = 1->3->4->6
* 返回的结果应该是[101,301,401,601]
* @param list
*/
public int[] getElements(LinkedList list){
checkList(list);
int[] dest = new int[list.size];
int arrayIndex = 0;
while (list.head != null) {
dest[arrayIndex] = (int)this.get((int)list.head.data);
++ arrayIndex;
list.head = list.head.next;
}
return dest;
}

private void checkList(LinkedList list) {
for (int i = 0; i <list.size; i++){
rangeCheck((int)list.get(i));
}

}
/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 从当前链表中中删除在list中出现的元素
* @param list
*/

public void subtract(LinkedList list){
if (list == null || list.size == 0 || this.size == 0) {
return;
}
int index = 0;
int listIndex = 0;
Node temp = head;
while (true) {
if((int)temp.data <= (int)list.get(listIndex)){
index ++;
temp = temp.next;
} else if ((int)temp.data == (int)list.get(listIndex)) {
this.remove(index);
index ++;
listIndex ++;
} else if (index >= this.size || listIndex >= list.size) {
break;
}
}
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){
if (this.size == 0) {
return;
}
Node subHead = head;
Node subTail = head;
while(true) {
if (subTail == null) {
subHead.next = null;
break;
}
if ((int)subTail.data == (int)subHead.data) {
if (!(subTail ==subHead)){
this.size --;
}
subTail = subTail.next;
} else {
subHead.next = subTail;
subHead = subHead.next;
}
}
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
* @param min
* @param max
*/
//一地bug。。。
public void removeRange(int min, int max){
if (this.size ==0) {
return;
}
if ((int)head.data > max) {
throw new IllegalArgumentException();
}
Node temp = head;
int index = 0;
int from = 0;
int to = 0;
while (temp != null) {
if ((int)temp.data <= min) {
temp = temp.next;
} else if ((int)temp.data > min && from == 0){
from = index;
}
if ((int)temp.data < max && from > 0) {
temp = temp.next;
} else if((int)temp.data > max && to == 0) {
to = index;
}
++ index;
}
if (to == 0) {
this.remove(from, this.size - from);
} else {
this.remove(from, to - from);
}
}

/**
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
* @param list
*/
public LinkedList intersection( LinkedList list){
if (this.size ==0 || list.size == 0){
return null;
}
Node tempHead = head;
int listIndex = 0;
LinkedList newList = new LinkedList();
while (true) {
if(tempHead == null || list.size < listIndex) {
break;
}
if ((int)tempHead.data < (int)list.get(listIndex)){
tempHead = head.next;
} else if ((int)tempHead.data > (int)list.get(listIndex)){
listIndex ++;
} else if ((int)tempHead.data == (int)list.get(listIndex)){
newList.add(tempHead);
tempHead = tempHead.next;
++ listIndex;
}
}

return newList;
}
}
39 changes: 39 additions & 0 deletions group01/349209948/src/week3_0312/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package week3_0312;

import java.io.IOException;
import java.io.RandomAccessFile;

import week3_0312.api.Connection;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;

public DownloadThread( Connection conn, int startPos, int endPos){

this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
}
public void run(){
byte[] data = new byte[endPos - startPos];
try {
data = conn.read(startPos, endPos);
} catch (IOException e) {
e.printStackTrace();
}
writeToFile(data);
}
public void writeToFile(byte[] data) {
RandomAccessFile file;
try {
file = new RandomAccessFile("downloadTest.jpg","rw");
file.seek(startPos);
file.write(data, 0, data.length);
} catch (IOException e) {
e.printStackTrace();
}
}
}
87 changes: 87 additions & 0 deletions group01/349209948/src/week3_0312/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package week3_0312;

import java.io.IOException;

import week3_0312.api.Connection;
import week3_0312.api.ConnectionException;
import week3_0312.api.ConnectionManager;
import week3_0312.api.DownloadListener;


public class FileDownloader {

String url;

DownloadListener listener;

ConnectionManager cm;


public FileDownloader(String _url) {
this.url = _url;

}

public void execute(){
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
// 具体的实现思路:
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
// 3. 把byte数组写入到文件中
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
Connection conn = null;
try {

conn = cm.open(this.url);

int length = conn.getContentLength();
DownloadThread downloadThread1 = new DownloadThread(conn,0,length/3);
downloadThread1.start();
DownloadThread downloadThread2 = new DownloadThread(conn, length/3+1, length/3 *2);
downloadThread2.start();
DownloadThread downloadThread3 = new DownloadThread(conn, length/3*2 +1, length -1);
downloadThread3.start();
try {
downloadThread1.join();
downloadThread2.join();
downloadThread3.join();
listener.notifyFinished();
} catch(InterruptedException e) {
e.printStackTrace();
}

} catch (ConnectionException e) {
e.printStackTrace();
}finally{
if(conn != null){
conn.close();
}
}




}

public void setListener(DownloadListener listener) {
this.listener = listener;
}



public void setConnectionManager(ConnectionManager ucm){
this.cm = ucm;
}

public DownloadListener getListener(){
return this.listener;
}

}
Loading

0 comments on commit 0a27f55

Please sign in to comment.