Skip to content

Commit

Permalink
Merge pull request diliuzuzhanghao#31 from liujk/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
wizardzhang2017 authored Mar 27, 2017
2 parents ff81ba6 + 9b2191b commit 355a2fb
Show file tree
Hide file tree
Showing 14 changed files with 745 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.Test;

public class ArrayListTest {

ArrayList<String> list;

@Before
Expand Down
153 changes: 145 additions & 8 deletions group27/1252327158/task1_20170312/src/com/coding/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ public Node(T data, Node<T> node) {
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse(){

if (size <= 1) {
return;
}
Node<T> node = head;
while (node.next != null) {
Node<T> temp = node.next;
node.next = temp.next;
temp.next = head;
head = temp;
}
}

/**
Expand All @@ -193,7 +202,11 @@ public void reverse(){
*/
public void removeFirstHalf(){

if (size < 2) {
return;
}
int delSize = (int)Math.floor(size/2);
remove(0, delSize);
}

/**
Expand All @@ -202,8 +215,31 @@ public void removeFirstHalf(){
* @param length
*/
public void remove(int i, int length){
if (i < 0 || i >= size || length < 0 || i + length > size) {
throw new IndexOutOfBoundsException();
}
if (i == 0) {
head = removeStartWith(head, length);
return;
}
Node beforeStart = head; //被删除元素的前一个
for (int index = 1; index < i; index++) {
beforeStart = beforeStart.next;
}
beforeStart.next = removeStartWith(beforeStart.next, length);
}

private Node<T> removeStartWith(Node<T> startNode, int length) {
Node<T> node = null;
for (int index = 1; index <= length; index++) {
node = startNode;
startNode = startNode.next;
node.next = null;
size--;
}
return startNode;
}

/**
* 假定当前链表和list均包含已升序排列的整数
* 从当前链表中取出那些list所指定的元素
Expand All @@ -212,8 +248,29 @@ public void remove(int i, int length){
* 返回的结果应该是[101,301,401,601]
* @param list
*/
public static int[] getElements(LinkedList list){
return null;
public int[] getElements(LinkedList list){
if (size == 0 || list == null || list.size == 0) {
return new int[0];
}
int[] result = new int[list.size];
Node node = head;
int index = 0;
int resultIndex = 0;
for (int i = 0; i < size; i++ ) {
int listData = ((Integer)list.get(index)).intValue();
if ( listData >= size) {
throw new IndexOutOfBoundsException();
}
if (i == listData) {
result[resultIndex++] = ((Integer)node.data).intValue();
index++;
}
if (index == list.size || listData == size) {
break;
}
node = node.next;
}
return result;
}

/**
Expand All @@ -224,15 +281,57 @@ public static int[] getElements(LinkedList list){
*/

public void subtract(LinkedList list){

if (list == null || list.size() == 0) {
return;
}
Node node = head;
Node beforeNode = null;
Node temp = null;
int j = 0; //参数list索引
for (;node != null && j < list.size() ;) {
int paradata = ((Integer)list.get(j)).intValue();
int data = ((Integer)node.data).intValue();
if (data == paradata) {
j++;
size--;
temp = node;
if (beforeNode == null) {
head = node.next;
node = node.next;
} else {;
beforeNode.next = node.next;
node = node.next;
}
temp.next = null;
} else if (data < paradata) {
beforeNode = node;
node = node.next;
} else {
j++;
}
}
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){

if (size < 2) {
return;
}
Node node = head;
Node delNode = null;
while (node.next != null) {
if (((Integer)node.next.data).equals(node.data)) {
delNode = node.next;
node.next = node.next.next;
delNode.next = null;
size--;
} else {
node = node.next;
}
}
}

/**
Expand All @@ -242,7 +341,27 @@ public void removeDuplicateValues(){
* @param max
*/
public void removeRange(int min, int max){

if (min >= max) {
return;
}
Node node = head;
int delLen = 0;
int startIndex = -1;
for (int i = 0; i < size; i++) {
int currentData = ((Integer)node.data).intValue();
if (currentData > min && currentData < max) {
if (delLen == 0) {
startIndex = i;
}
delLen++;
} else if (currentData >= max) {
break;
}
node = node.next;
}
if (delLen > 0) {
remove(startIndex, delLen);
}
}

/**
Expand All @@ -251,6 +370,24 @@ public void removeRange(int min, int max){
* @param list
*/
public LinkedList intersection( LinkedList list){
return null;
if (list.size() == 0 || size == 0) {
return null;
}
LinkedList result = new LinkedList();
Node node = head;
Iterator listIter = list.iterator();
while (listIter.hasNext()) {
int listData = ((Integer)listIter.next()).intValue();
for (;node != null;) {
int currentData = ((Integer)node.data).intValue();
if (currentData == listData) {
result.addLast(currentData);
} else if (currentData > listData) {
break;
}
node = node.next;
}
}
return result;
}
}
117 changes: 116 additions & 1 deletion group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import org.junit.Test;

public class LinkedListTest {
LinkedList<String> list;

LinkedList<String> list;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -92,4 +93,118 @@ public void testIterator() {
}
}

@Test
public void reverse() throws Exception {
list.add("third");
list.add("forth");
Assert.assertEquals("forth", list.get(0));
list.reverse();
Assert.assertEquals("first", list.get(0));
Assert.assertEquals("second", list.get(1));
Assert.assertEquals("third", list.get(2));
Assert.assertEquals("forth", list.get(3));
}

@Test
public void removeFirstHalf() throws Exception {
list.add("third");
list.add("forth");
list.removeFirstHalf();
Assert.assertEquals("second", list.get(0));
Assert.assertEquals(2, list.size());
}

@Test
public void remove() throws Exception {
list.add("third");
list.add("forth");
list.remove(1, 2);
Assert.assertEquals("forth", list.get(0));
Assert.assertEquals("first", list.get(1));
}

@Test
public void getElements() throws Exception {
LinkedList<Integer> intList = new LinkedList<>();
intList.addLast(11);
intList.addLast(101);
intList.addLast(201);
intList.addLast(301);
intList.addLast(401);
intList.addLast(501);
intList.addLast(601);
intList.addLast(701);
LinkedList<Integer> searchList = new LinkedList<>();
searchList.addLast(1);
searchList.addLast(3);
searchList.addLast(4);
searchList.addLast(7);

Assert.assertArrayEquals(new int[]{101,301,401,701}, intList.getElements(searchList));
}

@Test
public void subtract() throws Exception {
LinkedList<Integer> intList = new LinkedList<>();
intList.addLast(11);
intList.addLast(101);
intList.addLast(201);
intList.addLast(301);
intList.addLast(401);
LinkedList<Integer> delList= new LinkedList<>();
delList.addLast(11);
delList.addLast(101);
delList.addLast(301);
delList.addLast(401);
intList.subtract(delList);
Assert.assertEquals(201, ((Integer)intList.get(0)).intValue());
Assert.assertEquals(1, intList.size());
}

@Test
public void removeDuplicateValues() throws Exception {
LinkedList<Integer> intList = new LinkedList<>();
intList.addLast(11);
intList.addLast(101);
intList.addLast(101);
intList.addLast(101);
intList.addLast(401);
intList.removeDuplicateValues();
Assert.assertEquals(11, ((Integer)intList.get(0)).intValue());
Assert.assertEquals(101, ((Integer)intList.get(1)).intValue());
Assert.assertEquals(401, ((Integer)intList.get(2)).intValue());
Assert.assertEquals(3, intList.size());
}

@Test
public void removeRange() throws Exception {
LinkedList<Integer> intList = new LinkedList<>();
intList.addLast(11);
intList.addLast(101);
intList.addLast(201);
intList.addLast(301);
intList.addLast(401);
intList.removeRange(11, 301);
Assert.assertEquals(3, intList.size());
Assert.assertEquals(11, ((Integer)intList.get(0)).intValue());
Assert.assertEquals(301, ((Integer)intList.get(1)).intValue());
}

@Test
public void intersection() throws Exception {
LinkedList<Integer> intList = new LinkedList<>();
intList.addLast(11);
intList.addLast(101);
intList.addLast(201);
intList.addLast(301);
intList.addLast(401);
LinkedList<Integer> paraList= new LinkedList<>();
paraList.addLast(11);
paraList.addLast(301);
paraList.addLast(501);
LinkedList<Integer> newList = intList.intersection(paraList);
Assert.assertEquals(2, newList.size());
Assert.assertEquals(11, ((Integer)newList.get(0)).intValue());
Assert.assertEquals(301, ((Integer)newList.get(1)).intValue());
}
}
Loading

0 comments on commit 355a2fb

Please sign in to comment.