forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...2/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...ain/java/com/zhaogd/collection/Stack.java → ...va/com/zhaogd/collection/stack/Stack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/StackUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.