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
Expand Up @@ -40,6 +40,7 @@ public void add(int index , Object o){
newNode.setPre(pre);
newNode.setNext(indexNode);
indexNode.setPre(newNode);
size++;
}
private void checkIndex(int index){
if(index >= size || index <0){
Expand All @@ -56,8 +57,10 @@ private Node getNode(int index){
if(index==size-1){
return tail;
}
Node current = head;
for (int i = 0; i < index; i++) {
result = head.getNext();
result = current.getNext();
current = result;
}
return result;
}
Expand All @@ -79,6 +82,7 @@ public Object remove(int index){
}else{
tail = pre;
}
size--;
return indexNode.getData();
}

Expand All @@ -88,28 +92,54 @@ public int size(){

public void addFirst(Object o){
Node newNode = new Node(o);
if (size==0){
add(o);
return;
}
head.setPre(newNode);
newNode.setNext(head);
head = newNode;
size++;
}
public void addLast(Object o){
if (size == 0) {
add(o);
return;
}
Node newNode = new Node(o);
tail.setNext(newNode);
newNode.setPre(tail);
tail = newNode;
size++;
}
public Object removeFirst(){
if(size<1){
throw new IllegalArgumentException();
}
if(size==1){
tail=null;
}
Node next = head.getNext();
Node oldHead = head;
head = next;
head.setPre(null);
head = next;
oldHead.setNext(null);
size--;
return oldHead;
}
public Object removeLast(){
if(size<1){
throw new IllegalArgumentException();
}
if(size==1){
head=null;
}
Node oldTail = tail;
Node pre = tail.getPre();
tail = pre;
tail.setNext(null);
oldTail.setPre(null);
size--;
return oldTail;
}
public Iterator iterator(){
Expand Down Expand Up @@ -161,6 +191,28 @@ public void setPre(Node pre) {
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse(){
Node current = head;
Node next = current.getNext();
Node pre = current.getPre();
while(next!=null){
Node nNext = next.getNext();
if(pre!=null){
pre.setPre(current);
}
if(current!=null){
current.setPre(next);
current.setNext(pre);
}
if(next!=null){
next.setNext(current);
}
pre = current;
current = next;
next = nNext;
}
Node oldHead = head;
head = tail;
tail = oldHead;
}

/**
Expand All @@ -170,16 +222,54 @@ public void reverse(){

*/
public void removeFirstHalf(){

int removeSize = size/2;
for (int i = 0; i < removeSize; i++) {
removeFirst();
}
}

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
* @param i
* @param length
* @param start 开始位置
* @param length 长度
*/
public void remove(int i, int length){
public void remove(int start, int length){
checkIndex(start);
if(length<1){
return;
}
if(start==0 && length>=size){
int removeSum = size;
for (int j = 0; j < removeSum; j++) {
removeFirst();
}
size = size-length;
return;
}

int customMaxIndex = start+length;
int endIndex = customMaxIndex < size ? customMaxIndex : size;
if(start==0){
for (int j = 0; j < length; j++) {
removeFirst();
}
size = size-length;
return;
}
if(endIndex==size){
int removeSum = size-start;
for (int j = 0; j < removeSum; j++) {
removeLast();
}
return;
}
Node startNode = getNode(start-1);
Node endNode = getNode(endIndex);
startNode.getNext().setPre(null);
startNode.setNext(endNode);
endNode.getPre().setNext(null);
endNode.setPre(startNode);
size = size-length;
}
/**
* 假定当前链表和list均包含已升序排列的整数
Expand All @@ -189,8 +279,24 @@ public void remove(int i, int length){
* 返回的结果应该是[101,301,401,601]
* @param list
*/
public static int[] getElements(LinkedList list){
return null;
public int[] getElements(LinkedList list){
for (int i = 0; i < list.size; i++) {
if(Integer.parseInt(list.get(i).toString())>=this.size){
throw new IndexOutOfBoundsException();
}
}
int[] result = new int[list.size];
int index = 0;
for (int i = 0; i < this.size; i++) {
if (index==list.size()){
break;
}
if(Integer.parseInt(list.get(index).toString())==i){
result[index] = Integer.parseInt(this.get(i).toString());
index++;
}
}
return result;
}

/**
Expand All @@ -201,15 +307,47 @@ public static int[] getElements(LinkedList list){
*/

public void subtract(LinkedList list){

int index = 0;
Object compare = list.get(index);
Node current = head;
while(current!=null && compare!=null){
Node preCurrent = current;
current = current.getNext();
// TODO 这里不能删除重复元素只能删除一次
if(preCurrent.getData().equals(compare)){
preCurrent.getPre().setNext(preCurrent.getNext());
preCurrent.getNext().setPre(preCurrent.getPre());
preCurrent.setPre(null);
preCurrent.setNext(null);
size--;
compare = ++index < list.size ? list.get(index) : null;
}
}
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){

Node current = head;
Node next = current.getNext();
while (next!=null){
if(next.getData().equals(current.getData())){
Node preNext = next;
next = preNext.getNext();
current.setNext(preNext.getNext());
if (next != null) {
next.setPre(current);
}
preNext.setPre(null);
preNext.setNext(null);
size--;
}else{
current = next;
next = next.getNext();
}
}
}

/**
Expand All @@ -219,7 +357,23 @@ public void removeDuplicateValues(){
* @param max
*/
public void removeRange(int min, int max){

int minIndex = -1;
int maxIndex = -1;
int index = 0;
Node current = head;
while (current!=null){
if(current.getData().equals(min)){
minIndex = index;
}
if(current.getData().equals(max)){
maxIndex = index;
}
index++;
current = current.getNext();
}
if (minIndex > -1 && maxIndex > -1 && min <= max && minIndex + 1 < size) {
remove(minIndex + 1, maxIndex - minIndex - 1);
}
}

/**
Expand All @@ -230,4 +384,17 @@ public void removeRange(int min, int max){
public LinkedList intersection( LinkedList list){
return null;
}
@Override
public String toString(){
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("[");
for (int i = 0; i < size; i++) {
stringBuffer.append(get(i));
if(i!=size-1){
stringBuffer.append(",");
}
}
stringBuffer.append("]");
return stringBuffer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.wdn.coding2017.basic.lru;

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

/**
* 用双向链表实现LRU算法
* @author liuxin
*
*/
public class LRUPageFrame {
private int capacity;
private LinkedList cache = new LinkedList();
public LRUPageFrame(int capacity) {
this.capacity = capacity;
}

/**
* 获取缓存中对象
*
* @param pageNum
* @return
*/
public void access(int pageNum) {
if(capacity==cache.size()){
int exist = exist(pageNum);
if(exist>-1){
cache.addFirst(cache.remove(exist));
}else{
cache.removeLast();
cache.addFirst(pageNum);
}
}else {
cache.addFirst(pageNum);
}

}
private int exist(int pageNum){
for (int i = 0; i < cache.size(); i++) {
if(cache.get(i).equals(pageNum)){
return i;
}
}
return -1;
}
public String toString(){
return cache.toString().replace("[","").replace("]","");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.wdn.coding2017.basic.lru;

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

}
Loading