Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.wdn.coding2017.basic.stack;

import java.util.*;

/**
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值
* Created by Administrator on 2017/5/6 0006.
*/
public class QuickMinStack<E extends Comparable> {
private Stack<E> stack = new Stack<>();
private Map<Integer, E> minIndexMap = new HashMap<>();
private List<Integer> minIndexs = new ArrayList<>();

public static void main(String[] args) {
QuickMinStack quickMinStack = new QuickMinStack();
//int[] ints = {5,4,3,2,1,0};
int[] ints = {4,1,2,5,3,0};
for (int i = 0; i < ints.length; i++) {
quickMinStack.push(ints[i]);
}
quickMinStack.status();
while (!quickMinStack.isEmpty()){
System.out.print("min:"+quickMinStack.findMin());
System.out.println(" pop:"+quickMinStack.pop());
}
}
public void push(E data){
stack.push(data);
if(minIndexs.size()==0){
minIndexMap.put(0,data);
minIndexs.add(0);
}else {
if(findMin().compareTo(data)>0){
minIndexMap.put(stack.size()-1,data);
minIndexs.add(stack.size()-1);
}
}
}
public E pop(){
E min = findMin();
E result = stack.peek();
if(min==result){
minIndexMap.remove(minIndexs.get(minIndexs.size()-1));
minIndexs.remove(minIndexs.size()-1);
}
return stack.pop();
}
public E findMin(){
return minIndexMap.get(minIndexs.get(minIndexs.size()-1));
}
public boolean isEmpty(){
return stack.isEmpty();
}
public void status(){
System.out.println(stack);
System.out.println(minIndexs);
System.out.println(minIndexMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.wdn.coding2017.basic.stack;


import com.github.wdn.coding2017.basic.Queue;

/**
* Created by Administrator on 2017/5/6 0006.
*/
public class StackWithTwoQueues<E> {
Queue queue1 = new Queue();
Queue queue2 = new Queue();

public static void main(String[] args) {
StackWithTwoQueues<Integer> stack = new StackWithTwoQueues<>();
for (int i = 0; i < 5; i++) {
stack.push(i);
}
for (int i = 0; i < 3; i++) {
System.out.println(stack.pop());
}
System.out.println("");
for (int i = 0; i < 5; i++) {
stack.push(i);
}
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
public void push(E data){
if(queue1.isEmpty()){
queue2.enQueue(data);
}else{
queue1.enQueue(data);
}
}
public E pop(){
if(isEmpty()){
throw new RuntimeException("stack is empty");
}
if(queue1.isEmpty()){
int len = queue2.size();
for (int i = 0; i < len-1; i++) {
queue1.enQueue(queue2.deQueue());
}
return (E)queue2.deQueue();
}else{
int len = queue1.size();
for (int i = 0; i < len-1; i++) {
queue2.enQueue(queue1.deQueue());
}
return (E)queue1.deQueue();
}
}
public boolean isEmpty(){
return queue1.isEmpty() && queue2.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.github.wdn.coding2017.basic.stack;

import java.util.ArrayList;
import java.util.Arrays;

/**
* 用一个数组实现两个栈
* 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,
* 压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。
* Created by Administrator on 2017/5/6 0006.
*/
public class TwoStackInOneArray {
private Object[] ints = new Object[10];
private int head1 = -1;
private int head2 = ints.length;

public static void main(String[] args) {
TwoStackInOneArray stack = new TwoStackInOneArray();
for (int i = 11; i > 0; i--) {
stack.push2(i);
}
for (int i = 0; i < 8; i++) {
stack.push1(i);
}
stack.pop1();
for (int i = 0; i < 4; i++) {
stack.push1(i);
}

System.out.println(Arrays.toString(stack.ints));
}
public void push1(Object data){
if(head1+1<ints.length && ints[head1+1]==null){
ints[++head1] = data;
}else{
grow();
ints[++head1] = data;
}
}
public void push2(Object data){
if(head2-1>=0 && ints[head2-1]==null){
ints[--head2] = data;
}else{
grow();
ints[--head2] = data;
}
}
public Object pop1(){
if(head1<0){
throw new RuntimeException("stack1 is empty");
}
Object result = ints[head1];
ints[head1] = null;
head1--;
return result;
}
public Object pop2(){
if(head2>=ints.length){
throw new RuntimeException("stack2 is empty");
}
Object result = ints[head2];
ints[head2] = null;
head2++;
return result;
}
public void grow(){
Object[] newArr = new Object[ints.length * 2];
System.arraycopy(ints,0,newArr,0,head1+1);
System.arraycopy(ints, head2, newArr, newArr.length-(ints.length-head2), ints.length-head2);
head2 = newArr.length-(ints.length-head2);
ints = newArr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ public List<Method> getMethods() {
public void setMethods(List<Method> methods) {
this.methods = methods;
}

public Method getMainMethod() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.wdn.coding2017.jvm.engine;


import com.github.wdn.coding2017.jvm.method.Method;

public class ExecutionResult {
public static final int RUN_NEXT_CMD = 1;
public static final int JUMP = 2;
public static final int EXIT_CURRENT_FRAME = 3;
public static final int PAUSE_AND_RUN_NEW_FRAME = 4;

private int nextAction = RUN_NEXT_CMD;

private int nextCmdOffset = 0;

private Method nextMethod;

public Method getNextMethod() {
return nextMethod;
}
public void setNextMethod(Method nextMethod) {
this.nextMethod = nextMethod;
}



public void setNextAction(int action){
this.nextAction = action;
}
public boolean isPauseAndRunNewFrame(){
return this.nextAction == PAUSE_AND_RUN_NEW_FRAME;
}
public boolean isExitCurrentFrame(){
return this.nextAction == EXIT_CURRENT_FRAME;
}

public boolean isRunNextCmd(){
return this.nextAction == RUN_NEXT_CMD;
}

public boolean isJump(){
return this.nextAction == JUMP;
}

public int getNextCmdOffset() {
return nextCmdOffset;
}

public void setNextCmdOffset(int nextCmdOffset) {
this.nextCmdOffset = nextCmdOffset;
}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.wdn.coding2017.jvm.engine;

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

import com.github.wdn.coding2017.jvm.attr.CodeAttr;
import com.github.wdn.coding2017.jvm.clz.ClassFile;
import com.github.wdn.coding2017.jvm.cmd.ByteCodeCommand;
import com.github.wdn.coding2017.jvm.constant.MethodRefInfo;
import com.github.wdn.coding2017.jvm.method.Method;

public class ExecutorEngine {

private Stack<StackFrame> stack = new Stack<StackFrame>();

public ExecutorEngine() {

}

public void execute(Method mainMethod){



}



private void setupFunctionCallParams(StackFrame currentFrame,StackFrame nextFrame) {



}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.wdn.coding2017.jvm.engine;

public class Heap {

/**
* 没有实现垃圾回收, 所以对于下面新创建的对象, 并没有记录到一个数据结构当中
*/

private static Heap instance = new Heap();
private Heap() {
}
public static Heap getInstance(){
return instance;
}
public JavaObject newObject(String clzName){

JavaObject jo = new JavaObject(JavaObject.OBJECT);
jo.setClassName(clzName);
return jo;
}

public JavaObject newString(String value){
JavaObject jo = new JavaObject(JavaObject.STRING);
jo.setStringValue(value);
return jo;
}

public JavaObject newFloat(float value){
JavaObject jo = new JavaObject(JavaObject.FLOAT);
jo.setFloatValue(value);
return jo;
}
public JavaObject newInt(int value){
JavaObject jo = new JavaObject(JavaObject.INT);
jo.setIntValue(value);
return jo;
}

}
Loading