Skip to content

Commit

Permalink
Merge pull request #6 from eloiseSJTU/master
Browse files Browse the repository at this point in the history
老师的代码
  • Loading branch information
miniyk2012 authored Apr 16, 2017
2 parents 63c46f3 + 5e36919 commit f5b7a60
Show file tree
Hide file tree
Showing 39 changed files with 1,978 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private static class Node {

Node prev;
Node next;
int pageNum=10000;
Object pageNum=10000;
String flag;

Node() {
Expand Down Expand Up @@ -56,19 +56,19 @@ public boolean lookup(int pageNum){
Node nodel = last;

//判断要找的元素是否在第一个节点里
if (nodef.next.pageNum == pageNum){
if (nodef.next.pageNum.equals(pageNum)){
return true;
}

//判断要找的元素是否在最后一个节点里
if (nodel.prev.pageNum == pageNum){
if (nodel.prev.pageNum.equals(pageNum)){
remove(pageNum); //删除尾元素
addFirst(pageNum); //把访问pageNum添加到第一个节点里
return true;
}

while(nodef.next!=null&&nodef.next.pageNum != 999){
if (nodef.next.pageNum == pageNum){
while(nodef.next!=null&&!nodef.next.pageNum.equals(999)){
if (nodef.next.pageNum.equals(pageNum)){
nodef.next = nodef.next.next; //先删除元素
nodef.next.prev = nodef;
addFirst(pageNum); //再把访问添加元素
Expand All @@ -79,14 +79,30 @@ public boolean lookup(int pageNum){
return false;
}

public void remove(int pageNum){
public Node getFirst() {
return first;
}

public void setFirst(Node first) {
this.first = first;
}

public Node getLast() {
return last;
}

public void setLast(Node last) {
this.last = last;
}

public void remove(Object pageNum){
length--;
Node n = last.prev;
last.prev = n.prev;
n.prev.next = last;
}

public void addFirst(int pageNum){
public void addFirst(Object pageNum){
Node s = first.next;
Node n = new Node();
n.pageNum=pageNum;
Expand All @@ -95,7 +111,7 @@ public void addFirst(int pageNum){
first.next=n;
length++;
}

public String toString(){
StringBuilder buffer = new StringBuilder();
Node node = first;
Expand Down Expand Up @@ -130,4 +146,46 @@ public String lastToString(){
}
return buffer.toString();
}

public Object getFirstNode(){
return first.next.pageNum;
}

/**
* StackUtil 使用
* @param len
* @return
*/
public Object[] getElements(int len){
Object[] obj = new Object[len];
int i=0;
Node node = first;
while(node.next.flag != "last"){
obj[i] = node.next.pageNum;
node = node.next;
if (i == len-1){
return obj;
}
i++;
}
return null;
}

/**
* StackUtil 使用
* @param obj
* @param o
*/
public void remove(Object obj,Object o){
Node nodef = first;
while(nodef.next!=null&&!nodef.next.pageNum.equals(999)){
if (nodef.next.pageNum.equals(obj)){
nodef.next = nodef.next.next; //先删除元素
nodef.next.prev = nodef;
length--;
break;
}
nodef = nodef.next;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.orajavac.coding2017.basic.stack;

import com.github.orajavac.coding2017.basic.linklist.LRUPageFrame;

public class Stack {

private LRUPageFrame l;

public Stack(int capacity){
l = new LRUPageFrame(capacity);
}

public void push(Object o){
l.addFirst(o);
}

public String toString(){
return l.lastToString();
}

public Object[] getElements(int len){
return l.getElements(len);
}

public void remove(Object obj){
l.remove(obj, null);
}

public Object getFirstNode(){
return l.getFirstNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.github.orajavac.coding2017.basic.stack;

public class StackUtil {

/**
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*/
public static void reverse(Stack s) {
System.out.println(s.toString());
}

/**
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param o
*/
public static void remove(Stack s,Object o) {
s.remove(o);
}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
* @param len
* @return
*/
public static Object[] getTop(Stack s,int len) {
return s.getElements(len);
}
/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false;
* @param s
* @return
*/
public static boolean isValidPairs(Stack s,String str){
for (int i=0;i<str.length();i++){
char c1 = str.charAt(i);
if(isLeftCase(c1)){
s.push(c1); //左符号压栈
}else if (isRightCase(c1)){
Object obj = s.getFirstNode();
char[] c = obj.toString().toCharArray();
if(isCase(c[0],c1)){ //左、右匹配
s.remove(obj);
}else{
return false;
}
}
}
return true;
}

/**
* StackUtil 使用
* @param c1
* @param c2
* @return
*/
public static boolean isCase(char c1,char c2){
if(c1=='('&&c2==')')
return true;
if(c1=='{'&&c2=='}')
return true;
if(c1=='['&&c2==']')
return true;
return false;//其他的都返回false
};

/**
* StackUtil 使用
* @param c1
* @return
*/
public static boolean isLeftCase(char c1){
if(c1=='(')
return true;
if(c1=='{')
return true;
if(c1=='[')
return true;
return false;//其他的都返回false
};

/**
* StackUtil 使用
* @param c1
* @return
*/
public static boolean isRightCase(char c1){
if(c1==')')
return true;
if(c1=='}')
return true;
if(c1==']')
return true;
return false;//其他的都返回false
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.orajavac.coding2017.jvm.clz;

public class AccessFlag {
private int flagValue;

public AccessFlag(int value) {
this.flagValue = value;
}

public int getFlagValue() {
return flagValue;
}

public void setFlagValue(int flag) {
this.flagValue = flag;
}

public boolean isPublicClass(){
return (this.flagValue & 0x0001) != 0;
}
public boolean isFinalClass(){
return (this.flagValue & 0x0010) != 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.github.orajavac.coding2017.jvm.clz;

import com.github.orajavac.coding2017.jvm.constant.ClassInfo;
import com.github.orajavac.coding2017.jvm.constant.ConstantPool;

public class ClassFile {
private int minorVersion;
private int majorVersion;

private AccessFlag accessFlag;
private ClassIndex clzIndex;
private ConstantPool pool;


public ClassIndex getClzIndex() {
return clzIndex;
}
public AccessFlag getAccessFlag() {
return accessFlag;
}
public void setAccessFlag(AccessFlag accessFlag) {
this.accessFlag = accessFlag;
}



public ConstantPool getConstantPool() {
return pool;
}
public int getMinorVersion() {
return minorVersion;
}
public void setMinorVersion(int minorVersion) {
this.minorVersion = minorVersion;
}
public int getMajorVersion() {
return majorVersion;
}
public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}
public void setConstPool(ConstantPool pool) {
this.pool = pool;

}
public void setClassIndex(ClassIndex clzIndex) {
this.clzIndex = clzIndex;
}




public void print(){

if(this.accessFlag.isPublicClass()){
System.out.println("Access flag : public ");
}
System.out.println("Class Name:"+ getClassName());

System.out.println("Super Class Name:"+ getSuperClassName());


}

private String getClassName(){
int thisClassIndex = this.clzIndex.getThisClassIndex();
ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex);
return thisClass.getClassName();
}
private String getSuperClassName(){
ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.orajavac.coding2017.jvm.clz;

public class ClassIndex {
private int thisClassIndex;
private int superClassIndex;

public int getThisClassIndex() {
return thisClassIndex;
}
public void setThisClassIndex(int thisClassIndex) {
this.thisClassIndex = thisClassIndex;
}
public int getSuperClassIndex() {
return superClassIndex;
}
public void setSuperClassIndex(int superClassIndex) {
this.superClassIndex = superClassIndex;
}
}
Loading

0 comments on commit f5b7a60

Please sign in to comment.