Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
guodongym committed Apr 6, 2017
1 parent fa116c4 commit e2633d2
Show file tree
Hide file tree
Showing 29 changed files with 1,142 additions and 12 deletions.
11 changes: 11 additions & 0 deletions group12/377401843/learning/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
<version>1.6.1</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.zhaogd.collection;
package com.zhaogd.array;

import java.util.Arrays;

import com.zhaogd.collection.Iterator;
import com.zhaogd.collection.List;

public class ArrayList implements List {

private int size = 0;
Expand All @@ -13,7 +16,7 @@ public class ArrayList implements List {
*
* @Method add
* @param o
* @see com.guodong.datastructure.List#add(java.lang.Object)
* @see com.zhaogd.collection.guodong.datastructure.List#add(java.lang.Object)
*/
public void add(Object o) {
ensureCapacityInternal(size + 1);
Expand All @@ -27,7 +30,7 @@ public void add(Object o) {
* @Method add
* @param index
* @param o
* @see com.guodong.datastructure.List#add(int, java.lang.Object)
* @see com.zhaogd.collection.guodong.datastructure.List#add(int, java.lang.Object)
*/
public void add(int index, Object o) {
checkRangeForAdd(index);
Expand All @@ -46,7 +49,7 @@ public void add(int index, Object o) {
* @Method get
* @param index
* @return
* @see com.guodong.datastructure.List#get(int)
* @see com.zhaogd.collection.guodong.datastructure.List#get(int)
*/
public Object get(int index) {
checkRangeForGetOrRemove(index);
Expand All @@ -60,7 +63,7 @@ public Object get(int index) {
* @Method remove
* @param index
* @return
* @see com.guodong.datastructure.List#remove(int)
* @see com.zhaogd.collection.guodong.datastructure.List#remove(int)
*/
public Object remove(int index) {
checkRangeForGetOrRemove(index);
Expand All @@ -80,7 +83,7 @@ public Object remove(int index) {
*
* @Method size
* @return
* @see com.guodong.datastructure.List#size()
* @see com.zhaogd.collection.guodong.datastructure.List#size()
*/
public int size() {
return size;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zhaogd.collection;

import com.zhaogd.collection.linkedlist.LinkedList;

public class Queue {
private LinkedList element = new LinkedList();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.zhaogd.collection.linkedlist;


public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;

private int currentSize;
private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {
this.currentSize = 0;
this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {

Node node = find(pageNum);
//在该队列中存在, 则提到队列头
if (node != null) {

moveExistingNodeToHead(node);

} else{

node = new Node();
node.pageNum = pageNum;

// 缓存容器是否已经超过大小.
if (currentSize >= capacity) {
removeLast();

}

addNewNodetoHead(node);




}
}

private void addNewNodetoHead(Node node) {

if(isEmpty()){

node.prev = null;
node.next = null;
first = node;
last = node;

} else{
node.prev = null;
node.next = first;
first.prev = node;
first = node;
}
this.currentSize ++;
}

private Node find(int data){

Node node = first;
while(node != null){
if(node.pageNum == data){
return node;
}
node = node.next;
}
return null;

}






/**
* 删除链表尾部节点 表示 删除最少使用的缓存对象
*/
private void removeLast() {
Node prev = last.prev;
prev.next = null;
last.prev = null;
last = prev;
this.currentSize --;
}

/**
* 移动到链表头,表示这个节点是最新使用过的
*
* @param node
*/
private void moveExistingNodeToHead(Node node) {

if (node == first) {

return;
}
else if(node == last){
//当前节点是链表尾, 需要放到链表头
Node prevNode = node.prev;
prevNode.next = null;
last.prev = null;
last = prevNode;

} else{
//node 在链表的中间, 把node 的前后节点连接起来
Node prevNode = node.prev;
prevNode.next = node.next;

Node nextNode = node.next;
nextNode.prev = prevNode;


}

node.prev = null;
node.next = first;
first.prev = node;
first = node;

}
private boolean isEmpty(){
return (first == null) && (last == null);
}

public String toString(){
StringBuilder buffer = new StringBuilder();
Node node = first;
while(node != null){
buffer.append(node.pageNum);

node = node.next;
if(node != null){
buffer.append(",");
}
}
return buffer.toString();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.zhaogd.collection.linkedlist;

import org.junit.Assert;

import org.junit.Test;


public class LRUPageFrameTest {

@Test
public void testAccess() {
LRUPageFrame frame = new LRUPageFrame(3);
frame.access(7);
frame.access(0);
frame.access(1);
Assert.assertEquals("1,0,7", frame.toString());
frame.access(2);
Assert.assertEquals("2,1,0", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(3);
Assert.assertEquals("3,0,2", frame.toString());
frame.access(0);
Assert.assertEquals("0,3,2", frame.toString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
frame.access(5);
Assert.assertEquals("5,4,0", frame.toString());

}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.zhaogd.collection;
package com.zhaogd.collection.linkedlist;

import java.util.NoSuchElementException;

import com.zhaogd.collection.Iterator;
import com.zhaogd.collection.List;

public class LinkedList implements List {

private int size;
Expand All @@ -15,7 +18,7 @@ public class LinkedList implements List {
*
* @Method add
* @param o
* @see com.guodong.datastructure.List#add(java.lang.Object)
* @see com.zhaogd.collection.guodong.datastructure.List#add(java.lang.Object)
*/
public void add(Object o) {
linkLast(o);
Expand All @@ -27,7 +30,7 @@ public void add(Object o) {
* @Method add
* @param index
* @param o
* @see com.guodong.datastructure.List#add(int, java.lang.Object)
* @see com.zhaogd.collection.guodong.datastructure.List#add(int, java.lang.Object)
*/
public void add(int index, Object o) {
checkIndexForAdd(index);
Expand All @@ -54,7 +57,7 @@ public void add(int index, Object o) {
* @Method get
* @param index
* @return
* @see com.guodong.datastructure.List#get(int)
* @see com.zhaogd.collection.guodong.datastructure.List#get(int)
*/
public Object get(int index) {
checkIndexForGet(index);
Expand All @@ -71,7 +74,7 @@ public Object getLast() {
* @Method remove
* @param index
* @return
* @see com.guodong.datastructure.List#remove(int)
* @see com.zhaogd.collection.guodong.datastructure.List#remove(int)
*/
public Object remove(int index) {
checkIndexForGet(index);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.zhaogd.collection;
package com.zhaogd.collection.stack;

import com.zhaogd.collection.linkedlist.LinkedList;

public class Stack {
private LinkedList elementData = new LinkedList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.zhaogd.collection.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) {

}

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

}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
* @param len
* @return
*/
public static Object[] getTop(Stack s,int len) {
return null;
}
/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false;
* @param s
* @return
*/
public static boolean isValidPairs(String s){
return false;
}


}
Loading

0 comments on commit e2633d2

Please sign in to comment.