Skip to content

Commit

Permalink
第三周作业
Browse files Browse the repository at this point in the history
  • Loading branch information
zavier committed Mar 11, 2017
1 parent 4798a04 commit 7518745
Show file tree
Hide file tree
Showing 11 changed files with 694 additions and 0 deletions.
226 changes: 226 additions & 0 deletions group01/765324639/src/zavier/week01/basic/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,230 @@ private static class Node {
next = null;
}
}

// =========================第三周作业=========================

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

/**
* 删除一个单链表的前半部分 例如: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 beforeRemoveStartNode = head;
for (int j = 0; j < i - 1; j++) {
beforeRemoveStartNode = beforeRemoveStartNode.next;
}

Node removeEndNode = beforeRemoveStartNode;
for (int j = 0; j < length; j++) {
removeEndNode = removeEndNode.next;
}

beforeRemoveStartNode.next = removeEndNode.next;
}

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 arrayNum = 0;
Node temp = head;
int n = (int) list.get(0);
for (int i = 0; i < n; i++) {
temp = temp.next;
}
dest[arrayNum++] = (int) temp.data;

for (int i = 1; i < list.size; i++) {
int num = (int) list.get(i) - (int) list.get(i - 1);
for (int j = 0; j < num; j++) {
temp = temp.next;
}
dest[arrayNum++] = (int) temp.data;
}
return dest;
}

private void checkList(LinkedList list) {
for (int i = 0; i < list.size; i++) {
if ((int) list.get(i) < 0 || (int) list.get(i) >= size) {
throw new IllegalArgumentException("list中的元素位置越界");
}
}
}

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

public void subtract(LinkedList list) {
if (list == null || list.size == 0 || this.size == 0) {
return;
}

int thisIndex = 0;
int listIndex = 0;
Node temp = head;
while (true) { // 后续需要优化替换remove()方法
if ((int) temp.data < (int) list.get(listIndex)) {
temp = temp.next;
thisIndex++;
} else if ((int) temp.data == (int) list.get(listIndex)) {
this.remove(thisIndex);
temp = temp.next;
thisIndex++;
listIndex++;
} else {
listIndex++;
}

if (thisIndex >= 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) {
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
*/
public void removeRange(int min, int max) {
if (this.size == 0) {
return;
}

if ((int) head.data > max) {
throw new IllegalArgumentException();
}

int length = 0;
Node subList = new Node(null);
Node temp = subList;
while (true) {
if (head == null) {
break;
}
if ((int) head.data <= min || (int) head.data >= max) {
temp.next = head;
temp = temp.next;
length++;
}
head = head.next;
}
temp.next = null; // 去掉尾部多余数据
head = subList.next;
size = length;
}

/**
* 假设当前链表和参数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 || listIndex >= list.size) {
break;
}

if ((int) tempHead.data < (int) list.get(listIndex)) {
tempHead = tempHead.next;
} else if ((int) tempHead.data > (int) list.get(listIndex)) {
listIndex++;
} else {
newList.add(tempHead.data);

tempHead = tempHead.next;
listIndex++;
}
}

return newList;
}
}
150 changes: 150 additions & 0 deletions group01/765324639/src/zavier/week01/test/LinkedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,154 @@ public void testRemoveLast() {
Assert.assertEquals(498, linkedList.size());
}

// =========================第三周作业=========================

@Test
public void testReverse() {
linkedList.reverse();
for (int i = 0; i < linkedList.size(); i++) {
Assert.assertEquals(499 - i, linkedList.get(i));
}
Assert.assertEquals(500, linkedList.size());
}

@Test
public void testRemoveFirstHalfOfEven() {
linkedList.removeFirstHalf();
for (int i = 250; i < 500; i++) {
Assert.assertEquals(i, linkedList.get(i - 250));
}
Assert.assertEquals(250, linkedList.size());
}

@Test
public void testRemoveFirstHalfOfOdd() {
linkedList.add(500);
linkedList.removeFirstHalf();
for (int i = 250; i < 501; i++) {
Assert.assertEquals(i, linkedList.get(i - 250));
}
Assert.assertEquals(251, linkedList.size());
}

@Test
public void testRemoveIntInt() {
linkedList.remove(10, 10);
Assert.assertEquals(0, linkedList.get(0));
Assert.assertEquals(20, linkedList.get(10));
Assert.assertEquals(490, linkedList.size());
}

@Test(expected = IllegalArgumentException.class)
public void testIllegalIRemoveIntInt() {
linkedList.remove(-10, 10);
}

@Test
public void testIllegalLengthRemoveIntInt() {
linkedList.remove(0, 10);
Assert.assertEquals(490, linkedList.size());
Assert.assertEquals(10, linkedList.get(0));

linkedList.remove(300, 500);
Assert.assertEquals(300, linkedList.size());
for (int i = 0; i < 300; i++) {
Assert.assertEquals(i + 10, linkedList.get(i));
}
}

@Test(expected = IllegalArgumentException.class)
public void testGetElements() {
LinkedList list = new LinkedList();
list.add(5);
list.add(10);
list.add(202);
list.add(305);
Assert.assertArrayEquals(new int[] {5, 10, 202, 305}, linkedList.getElements(list));

Assert.assertEquals(500, linkedList.size());

list.add(500);
linkedList.getElements(list);
}

@Test
public void testSubtract() {
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(5);
list1.add(8);
list1.add(10);
LinkedList list2 = new LinkedList();
list2.add(2);
list2.add(5);
list2.add(7);
list2.add(11);
list1.subtract(list2);

Assert.assertEquals(1, list1.get(0));
Assert.assertEquals(8, list1.get(1));
Assert.assertEquals(10, list1.get(2));
Assert.assertEquals(3, list1.size());
}

@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(4);
list.add(5);
list.add(6);
list.add(6);
list.add(7);
list.add(7);
list.add(7);

list.removeDuplicateValues();
for (int i = 0; i < 7; i++) {
Assert.assertEquals(i + 1, list.get(i));
}
Assert.assertEquals(7, list.size());
}

@Test
public void testRemoveRange() {
linkedList.removeRange(100, 300);
Assert.assertEquals(301, linkedList.size());
for (int i = 0; i < 100; i++) {
Assert.assertEquals(i, linkedList.get(i));
}
for (int i = 300; i < 500; i++) {
Assert.assertEquals(i, linkedList.get(i - 199));
}
}

@Test
public void testIntersection() {
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(2);
list1.add(5);
list1.add(7);
list1.add(9);

LinkedList list2 = new LinkedList();
list2.add(0);
list2.add(2);
list2.add(9);
list2.add(10);
list2.add(19);

LinkedList intersection = list1.intersection(list2);
Assert.assertNotNull(intersection);
Assert.assertEquals(2, intersection.size());
Assert.assertEquals(2, (int) intersection.get(0));
Assert.assertEquals(9, (int) intersection.get(1));


}
}
Loading

0 comments on commit 7518745

Please sign in to comment.