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
10 changes: 5 additions & 5 deletions group22/627559964/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.coding.basic;

/**
* �Զ��������
* 自定义二叉树
*
* @author xiongrui233
*
*/
public class BinaryTreeNode {

//�ڵ�ֵ
//节点值
private Object data;
//������
//左子树
private BinaryTreeNode left;
//������
//右子树
private BinaryTreeNode right;

public Object getData() {
Expand Down Expand Up @@ -40,7 +40,7 @@ public void setRight(BinaryTreeNode right) {
}

/**
* ����Ԫ��
* 插入元素
* @param o
* @return BinaryTreeNode
*/
Expand Down
4 changes: 3 additions & 1 deletion group22/627559964/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.coding.basic;

import com.coding.basic.linklist.LinkedList;

/**
* �Զ������
* 自定义队列
*
* @author xiongrui233
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package com.coding.basic;
package com.coding.basic.array;

import java.util.Arrays;

import com.coding.basic.Iterator;
import com.coding.basic.List;

/**
* �Զ���ArrayList
* 自定义ArrayList
*
* @author xiongrui233
*
*/
public class ArrayList implements List {

// list����
// list长度
private int size = 0;

// list��Ԫ�ؼ���
// list的元素集合
private Object[] elementData = new Object[10];

/**
* �ϲ�����
* 合并数组
*
* @param arrays1
* @param arrays2
Expand All @@ -31,7 +34,7 @@ private Object[] concat(Object[] arrays1, Object[] arrays2) {
}

/**
* �ָ�����
* 分割数组
*
* @param arrays
* @param from
Expand All @@ -47,8 +50,8 @@ private Object[] subArrays(Object[] arrays, int from, int index) {
}

/**
* ��̬����list����
* ����Ϊ:newSize = oldSize * 1.5
* 动态增长list长度
* 策略为:newSize = oldSize * 1.5
*
* @param oldSize
*/
Expand All @@ -57,7 +60,7 @@ private void grow(int oldSize) {
}

/**
* ����ڲ�����Ԫ��ʱ,list�����Ƿ��㹻
* 检查在插入新元素时,list长度是否足够
*
* @param newSize
*/
Expand All @@ -69,7 +72,7 @@ private void checkSize(int newSize) {
}

/**
* ����Ԫ��
* 新增元素
*
* @param Object
*/
Expand All @@ -79,7 +82,7 @@ public void add(Object o) {
}

/**
* ����Ԫ��
* 新增元素
*
* @param index
* @param Object
Expand All @@ -96,7 +99,7 @@ public void add(int index, Object o) {
}

/**
* ��ñ��Ϊindex��Ԫ��
* 获得编号为index的元素
*
* @param int
* @return Object
Expand All @@ -106,7 +109,7 @@ public Object get(int index) {
}

/**
* ɾ�����Ϊindex��Ԫ��
* 删除编号为index的元素
*
* @param int
* @return Object
Expand All @@ -122,7 +125,7 @@ public Object remove(int index) {
}

/**
* ����list����
* 返回list长度
*
* @return int
*/
Expand All @@ -131,7 +134,7 @@ public int size() {
}

/**
* �������
* 重写迭代器
*
* @return IteratorImpl
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.coderising.array;
package com.coding.basic.array;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
151 changes: 151 additions & 0 deletions group22/627559964/src/com/coding/basic/linklist/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.coding.basic.linklist;

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();
}

}
35 changes: 35 additions & 0 deletions group22/627559964/src/com/coding/basic/linklist/LRUPageFrameTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coding.basic.linklist;


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,4 +1,7 @@
package com.coding.basic;
package com.coding.basic.linklist;

import com.coding.basic.Iterator;
import com.coding.basic.List;

/**
* 自定义LinkList
Expand Down
Loading