Skip to content

Commit

Permalink
Merge pull request onlyliuxin#81 from onlyliuxin/master
Browse files Browse the repository at this point in the history
update-2017-04-24
  • Loading branch information
zavier authored Apr 24, 2017
2 parents c323730 + e062976 commit 67a8222
Show file tree
Hide file tree
Showing 52 changed files with 1,645 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package com.coding.basic.queue;

public class CircleQueue {
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;

public boolean isEmpty() {
return false;

}

public int size() {
return -1;
}



public void enQueue(E data) {

}

public E deQueue() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,35 @@

/**
* 用Queue来实现Josephus问题
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 知道最后一个人留下来
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来
* @author liuxin
*
*/
public class Josephus {

public static List<Integer> execute(int n, int m){

return null;
Queue<Integer> queue = new Queue<Integer>();
for (int i = 0; i < n; i++){
queue.enQueue(i);
}

List<Integer> result = new ArrayList<Integer>();
int i = 0;

while (!queue.isEmpty()) {

int x = queue.deQueue();

if (++i % m == 0){
result.add(x);
} else{
queue.enQueue(x);
}
}


return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.coding.basic.queue;

import java.util.NoSuchElementException;
import java.util.Stack;

public class QueueWithTwoStacks<E> {
private Stack<E> stack1;
private Stack<E> stack1;
private Stack<E> stack2;


Expand All @@ -13,29 +14,42 @@ public QueueWithTwoStacks() {
}



private void moveStack1ToStack2() {
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}

}


public boolean isEmpty() {
return false;
return stack1.isEmpty() && stack2.isEmpty();
}



public int size() {
return -1;
return stack1.size() + stack2.size();
}



public void enQueue(E item) {

stack1.push(item);
}

public E deQueue() {
return null;
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
if (stack2.isEmpty()) {
moveStack1ToStack2();
}

return stack2.pop();
}



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coding.basic.stack;

public class Tail {

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package com.coding.basic.stack.expr;

import java.util.List;
import java.util.Stack;

public class InfixToPostfix {

public static List<Token> convert(String expr) {
return null;
return null;
}

public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
String expr = "1+((2+3)*4)-5";
for(int i=0;i<expr.length(); i++){
char c = expr.charAt(i);
if(c == '+'){
stack.push(c);
}
else if(c == '*'){
stack.push(c);
}
else if(c == ')'){
System.out.print(stack.pop() + " ");
}
else if(c == '('){
System.out.print(" ");
} else{
System.out.print(c +" ");
}
}
System.out.println("");
}



}
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package com.coding.basic.queue;

public class CircleQueue {
/**
* 用数组实现循环队列
* @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;

public boolean isEmpty() {
return false;

}

public int size() {
return -1;
}



public void enQueue(E data) {

}

public E deQueue() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.coding.basic.queue;

import java.util.ArrayList;
import java.util.List;

/**
* 用Queue来实现Josephus问题
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 知道最后一个人留下来
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来
* 该方法返回一个List, 包含了被杀死人的次序
* @author liuxin
*
*/
public class Josephus {

public static List<Integer> execute(int n, int m){

public static List<Integer> execute(int n, int m){
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import java.util.Stack;

/**
* 用两个栈来实现一个队列
* @author liuxin
*
* @param <E>
*/
public class QueueWithTwoStacks<E> {
private Stack<E> stack1;
private Stack<E> stack1;
private Stack<E> stack2;


Expand All @@ -13,16 +19,16 @@ public QueueWithTwoStacks() {
}




public boolean isEmpty() {
return false;
return false;
}



public int size() {
return -1;
return -1;
}


Expand All @@ -36,5 +42,6 @@ public E deQueue() {
}



}

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
//buffer.append("Code:").append(code).append("\n");
for(int i=0;i<cmds.length;i++){
buffer.append(cmds[i].toString(pool)).append("\n");
buffer.append(cmds[i].toString()).append("\n");
}
buffer.append("\n");
buffer.append(this.lineNumTable.toString());
Expand Down
11 changes: 1 addition & 10 deletions liuxin/mini-jvm/answer/src/com/coderising/jvm/clz/ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ public Method getMethod(String methodName, String paramAndReturnType){
return null;
}
public Method getMainMethod(){
for(Method m :methods){
int nameIndex = m.getNameIndex();
int descIndex = m.getDescriptorIndex();
String name = this.getConstantPool().getUTF8String(nameIndex);
String desc = this.getConstantPool().getUTF8String(descIndex);
if(name.equals("main") && desc.equals("([Ljava/lang/String;)V")){
return m;
}
}
return null;
return getMethod("main","([Ljava/lang/String;)V");
}
}
15 changes: 12 additions & 3 deletions liuxin/mini-jvm/answer/src/com/coderising/jvm/cmd/BiPushCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.coderising.jvm.clz.ClassFile;
import com.coderising.jvm.constant.ConstantInfo;
import com.coderising.jvm.constant.ConstantPool;
import com.coderising.jvm.engine.ExecutionResult;
import com.coderising.jvm.engine.Heap;
import com.coderising.jvm.engine.JavaObject;
import com.coderising.jvm.engine.StackFrame;


public class BiPushCmd extends OneOperandCmd {
Expand All @@ -13,11 +17,16 @@ public BiPushCmd(ClassFile clzFile,String opCode) {
}

@Override
public String toString(ConstantPool pool) {
public String toString() {

return this.getOffset()+": "+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
return this.getOffset()+":"+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
}
public void execute(StackFrame frame,ExecutionResult result){
int value = this.getOperand();
JavaObject jo = Heap.getInstance().newInt(value);
frame.getOprandStack().push(jo);

}



}
Loading

0 comments on commit 67a8222

Please sign in to comment.