forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onlyliuxin#70 from ipk2015/master
121111914:jvm引擎及queue练习
- Loading branch information
Showing
35 changed files
with
1,162 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="E:/javaImprove/git/group24/121111914/lib/dom4j-1.6.1.jar" sourcepath="E:/javaImprove/git/group24/121111914/lib/dom4j-1.6.1-sources.jar"/> | ||
<classpathentry kind="lib" path="lib/commons-io-2.5.jar"/> | ||
<classpathentry kind="lib" path="lib/commons-lang3-3.5.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/> | ||
<classpathentry kind="lib" path="lib/commons-io-2.5.jar"/> | ||
<classpathentry kind="lib" path="lib/commons-lang3-3.5.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
158 changes: 81 additions & 77 deletions
158
...b/ipk2015/coding2017/basic/ArrayList.java → ...015/coding2017/basic/array/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,81 @@ | ||
package com.github.ipk2015.coding2017.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
add(size,o); | ||
} | ||
/* | ||
* 分两种情况,index的范围为0到size,超出则抛出异常 | ||
* */ | ||
public void add(int index, Object o){ | ||
ListUtils.checkIndexInRange(index,size); | ||
if(size==elementData.length){ | ||
elementData=Arrays.copyOf(elementData, size+1); | ||
} | ||
if(index<size){ | ||
for(int i=size;i>index;i--){ | ||
elementData[i]=elementData[i-1]; | ||
} | ||
} | ||
elementData[index]=o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
Object object=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
elementData[size-1]=null; | ||
size--; | ||
return object; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
return new Iterator(){ | ||
private int currentPos=0; | ||
@Override | ||
public boolean hasNext() { | ||
if(size==0){ | ||
return false; | ||
}else{ | ||
if(currentPos<size){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object object= get(currentPos); | ||
currentPos++; | ||
return object; | ||
} | ||
|
||
}; | ||
} | ||
} | ||
package com.github.ipk2015.coding2017.basic.array; | ||
|
||
import java.util.Arrays; | ||
|
||
import com.github.ipk2015.coding2017.basic.Iterator; | ||
import com.github.ipk2015.coding2017.basic.List; | ||
import com.github.ipk2015.coding2017.basic.ListUtils; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
add(size,o); | ||
} | ||
/* | ||
* 分两种情况,index的范围为0到size,超出则抛出异常 | ||
* */ | ||
public void add(int index, Object o){ | ||
ListUtils.checkIndexInRange(index,size); | ||
if(size==elementData.length){ | ||
elementData=Arrays.copyOf(elementData, size+1); | ||
} | ||
if(index<size){ | ||
for(int i=size;i>index;i--){ | ||
elementData[i]=elementData[i-1]; | ||
} | ||
} | ||
elementData[index]=o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
Object object=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
elementData[size-1]=null; | ||
size--; | ||
return object; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
return new Iterator(){ | ||
private int currentPos=0; | ||
@Override | ||
public boolean hasNext() { | ||
if(size==0){ | ||
return false; | ||
}else{ | ||
if(currentPos<size){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object object= get(currentPos); | ||
currentPos++; | ||
return object; | ||
} | ||
|
||
}; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...oding2017/coderising/array/ArrayUtil.java → ...015/coding2017/basic/array/ArrayUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.github.ipk2015.coding2017.coderising.array; | ||
package com.github.ipk2015.coding2017.basic.array; | ||
|
||
|
||
|
||
|
2 changes: 1 addition & 1 deletion
2
...g2017/coderising/array/ArrayUtilTest.java → ...coding2017/basic/array/ArrayUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
group24/121111914/src/com/github/ipk2015/coding2017/basic/queue/CircleQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.github.ipk2015.coding2017.basic.queue; | ||
|
||
|
||
|
||
/** | ||
* 用数组实现循环队列 | ||
* @author liuxin | ||
* | ||
* @param <E> | ||
*/ | ||
public class CircleQueue <E> { | ||
|
||
private final static int DEFAULT_SIZE = 10; | ||
|
||
//用数组来保存循环队列的元素 | ||
private Object[] elementData = new Object[DEFAULT_SIZE] ; | ||
|
||
//队头 | ||
private int front = 0; | ||
//队尾 | ||
private int rear = 0; | ||
private int size = 0; | ||
public boolean isEmpty() { | ||
return size == 0; | ||
|
||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
|
||
public void enQueue(E data) { | ||
if(size == DEFAULT_SIZE){ | ||
throw new RuntimeException("queue size is max"); | ||
} | ||
elementData[rear] = data; | ||
rear = (rear+1)%DEFAULT_SIZE; | ||
size++; | ||
} | ||
|
||
public E deQueue() { | ||
if(size == 0){ | ||
throw new RuntimeException("queue size is 0"); | ||
} | ||
E e = (E)elementData[front]; | ||
elementData[front] = null; | ||
front = (front+1)%DEFAULT_SIZE; | ||
size--; | ||
return e; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
group24/121111914/src/com/github/ipk2015/coding2017/basic/queue/Josephus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.ipk2015.coding2017.basic.queue; | ||
|
||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 用Queue来实现Josephus问题 | ||
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 | ||
* 该方法返回一个List, 包含了被杀死人的次序 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class Josephus { | ||
|
||
public static List<Integer> execute(int n, int m){ | ||
CircleQueue queue = new CircleQueue(); | ||
for(int i = 0;i<n;i++){ | ||
queue.enQueue(i); | ||
} | ||
int i=1; | ||
List<Integer> list = new ArrayList<Integer>(); | ||
Integer element = null; | ||
while(!queue.isEmpty()){ | ||
element = (Integer) queue.deQueue(); | ||
if(i == m){ | ||
list.add(element); | ||
i = 1; | ||
}else{ | ||
queue.enQueue(element); | ||
i++; | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
group24/121111914/src/com/github/ipk2015/coding2017/basic/queue/JosephusTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.ipk2015.coding2017.basic.queue; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
public class JosephusTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testExecute() { | ||
|
||
Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); | ||
|
||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ithub/ipk2015/coding2017/basic/Queue.java → ...ipk2015/coding2017/basic/queue/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
group24/121111914/src/com/github/ipk2015/coding2017/basic/queue/QueueWithTwoStacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.ipk2015.coding2017.basic.queue; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* 用两个栈来实现一个队列 | ||
* @author liuxin | ||
* | ||
* @param <E> | ||
*/ | ||
public class QueueWithTwoStacks<E> { | ||
private Stack<E> stack1; | ||
private Stack<E> stack2; | ||
|
||
|
||
public QueueWithTwoStacks() { | ||
stack1 = new Stack<E>(); | ||
stack2 = new Stack<E>(); | ||
} | ||
|
||
|
||
|
||
|
||
public boolean isEmpty() { | ||
|
||
return stack1.isEmpty() && stack2.isEmpty(); | ||
} | ||
|
||
|
||
|
||
public int size() { | ||
return stack1.size()+stack2.size(); | ||
} | ||
|
||
|
||
|
||
public void enQueue(E item) { | ||
stack1.push(item); | ||
} | ||
|
||
public E deQueue() { | ||
if(size() == 0){ | ||
throw new RuntimeException("queue size is 0"); | ||
} | ||
if(stack2.isEmpty()){ | ||
while(!stack1.isEmpty()){ | ||
stack2.push(stack1.pop()); | ||
} | ||
} | ||
return stack2.pop(); | ||
} | ||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.