Skip to content

Commit

Permalink
Merge pull request onlyliuxin#70 from ipk2015/master
Browse files Browse the repository at this point in the history
121111914:jvm引擎及queue练习
  • Loading branch information
DonaldY authored Apr 30, 2017
2 parents 0880c4d + 2c0eaa6 commit 1f97eb1
Show file tree
Hide file tree
Showing 35 changed files with 1,162 additions and 106 deletions.
20 changes: 10 additions & 10 deletions group24/121111914/.classpath
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>
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;
}

};
}
}
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;



Expand Down
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;

import static org.junit.Assert.*;

Expand Down
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;
}
}
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;
}

}
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());

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.ipk2015.coding2017.basic;
package com.github.ipk2015.coding2017.basic.queue;

import com.github.ipk2015.coding2017.basic.linkedlist.LinkedList;

Expand Down
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();
}



}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.EmptyStackException;

import com.github.ipk2015.coding2017.basic.ArrayList;
import com.github.ipk2015.coding2017.basic.array.ArrayList;



public class Stack {
private ArrayList elementData = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.junit.Before;
import org.junit.Test;

import com.github.ipk2015.coding2017.basic.ArrayList;
import com.github.ipk2015.coding2017.basic.array.ArrayList;



public class ArrayListTest {
ArrayList list;
Expand Down
Loading

0 comments on commit 1f97eb1

Please sign in to comment.