Skip to content

Commit

Permalink
Merge pull request luoziyihao#51 from thlcly/master
Browse files Browse the repository at this point in the history
完成第三次作业
  • Loading branch information
zavier authored Mar 10, 2017
2 parents 7e0c2e5 + 5552a0a commit 4798a04
Show file tree
Hide file tree
Showing 15 changed files with 640 additions and 7 deletions.
9 changes: 9 additions & 0 deletions group01/954958168/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build
target/
*.class
# ide
.settings/
.project
.classpath
.idea/
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Object[] toArray() {
Object[] objects = new Object[size];
Node first = head.next;
int pos = 0;
while (first!= null) {
while (first != null) {
objects[pos++] = first.data;
first = first.next;
}
Expand Down Expand Up @@ -126,4 +126,196 @@ private Node(Object data) {
this.data = data;
}
}

/**
* 把该链表逆置
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse() {
Node newHead = new Node(null);
Node node = head.next;
while (node != null) {
Node temp = node.next;
node.next = newHead.next;
newHead.next = node;
node = temp;
}
head = newHead;
}

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

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
*
* @param i
* @param length
*/
public void remove(int i, int length) {
if (i < 0 || i >= size || length < 1) throw new IndexOutOfBoundsException("索引超出范围");
int endIndex = Math.min(i + length, size);
Node preNode = head;
Node endNode = head.next;
for (int index = 0; index < endIndex; index++) {
if (index < i) {
preNode = endNode;
}
endNode = endNode.next;
}

preNode.next = endNode;
size = size - (endIndex - i);
}

/**
* 假定当前链表和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) {
if (list == null) return new int[0];
Iterator iterator = list.iterator();
int[] result = new int[list.size()];
int index = 0;
while (iterator.hasNext()) {
Integer next = (Integer) iterator.next();
result[index] = (Integer) this.get(next);
index++;
}
return result;
}

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

public void subtract(LinkedList list) {
if (list == null) return;
Iterator iterator = this.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
if (contain(element, list)) {
iterator.remove();
}
}
}

private boolean contain(Object element, LinkedList list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
if (next == element) return true;
}
return false;
}

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

}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
*
* @param min
* @param max
*/
public void removeRange(int min, int max) {
if (min >= max) return;
Node cur = head;
Node start = null;
Node end = null;
Integer startIndex = null;
Integer endIndex = size - 1;
Integer index = -1;
while (cur.next != null) {
if (start == null && (Integer) cur.next.data > min) {
start = cur;
startIndex = index;
}
if ((Integer) cur.next.data >= max) {
end = cur.next;
endIndex = index;
break;
}
cur = cur.next;
index++;
}
if (start != null) {
start.next = end;
size = size - (endIndex - startIndex);
}
}

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

return intersection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public void init() {
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
}

@Test
Expand Down Expand Up @@ -78,4 +80,104 @@ public void testIterator() {
}
Assert.assertArrayEquals(new Object[]{}, linkedList.toArray());
}

@Test
public void testReverse() {
linkedList.reverse();
Assert.assertArrayEquals(new Object[]{3, 2, 1}, linkedList.toArray());
}

@Test
public void testRemoveFirstHalf() {
linkedList.removeFirstHalf();
Assert.assertArrayEquals(new Object[]{2, 3}, linkedList.toArray());
}

@Test
public void testRangeRemove() {
linkedList.remove(1, 4);
Assert.assertArrayEquals(new Object[]{1}, linkedList.toArray());
}

@Test
public void testGetElements() {
LinkedList sub = new LinkedList();
sub.add(1);
sub.add(3);
sub.add(4);
int[] result = linkedList.getElements(sub);
Assert.assertArrayEquals(new int[]{2, 4, 5}, result);
}

@Test
public void testSubtract() {
LinkedList sub = new LinkedList();
sub.add(1);
sub.add(2);
sub.add(10);
linkedList.subtract(sub);
Assert.assertArrayEquals(new Object[]{3, 4, 5}, linkedList.toArray());
}

@Test
public void testRemoveDuplicateValues() {
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(2);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(6);
list.add(7);
list.add(7);
list.add(7);
list.add(7);
list.add(7);
list.removeDuplicateValues();
Assert.assertArrayEquals(new Object[]{1, 2, 3, 4, 5, 6, 7}, list.toArray());
}

@Test
public void testRemoveRange() {
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(7);
list.add(8);
list.removeRange(1, 4);
Assert.assertArrayEquals(new Object[]{1, 4, 5, 6, 7, 7, 8}, list.toArray());
}

@Test
public void testIntersection() {
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(3);
list1.add(5);
list1.add(7);
list1.add(9);
LinkedList list2 = new LinkedList();
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list2.add(10);
list2.add(11);
list2.add(12);
LinkedList intersection = list1.intersection(list2);
Assert.assertArrayEquals(new Object[]{3, 5, 7}, intersection.toArray());
}

}
20 changes: 14 additions & 6 deletions group01/954958168/class02/LiteStruts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
<artifactId>lite-struts</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand All @@ -29,6 +24,19 @@
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

</dependencies>

<profiles>
<profile>
<id>liteStruts</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</profile>
</profiles>
</project>
32 changes: 32 additions & 0 deletions group01/954958168/class03/MultiThreadDownload/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.aaront.exercise</groupId>
<artifactId>multithread-download</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>multiThreadDownload</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</profile>
</profiles>

</project>
Loading

0 comments on commit 4798a04

Please sign in to comment.