Skip to content

Commit 8e13893

Browse files
Merge pull request #200 from hdkn235/master
添加基于单链表实现LRU算法(java版)
2 parents ea105e0 + ebbaab8 commit 8e13893

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package linked.singlelist;
2+
3+
4+
import java.util.Scanner;
5+
6+
/**
7+
* 基于单链表LRU算法(java)
8+
*
9+
* @author hoda
10+
* @create 2018-12-17
11+
*/
12+
public class LRUBaseLinkedList<T> {
13+
14+
/**
15+
* 默认链表容量
16+
*/
17+
private final static Integer DEFAULT_CAPACITY = 10;
18+
19+
/**
20+
* 头结点
21+
*/
22+
private SNode<T> headNode;
23+
24+
/**
25+
* 链表长度
26+
*/
27+
private Integer length;
28+
29+
/**
30+
* 链表容量
31+
*/
32+
private Integer capacity;
33+
34+
public LRUBaseLinkedList() {
35+
this.headNode = new SNode<>();
36+
this.capacity = DEFAULT_CAPACITY;
37+
this.length = 0;
38+
}
39+
40+
public LRUBaseLinkedList(Integer capacity) {
41+
this.headNode = new SNode<>();
42+
this.capacity = capacity;
43+
this.length = 0;
44+
}
45+
46+
public void add(T data) {
47+
SNode preNode = findPreNode(data);
48+
49+
// 链表中存在,删除原数据,再插入到链表的头部
50+
if (preNode != null) {
51+
deleteElemOptim(preNode);
52+
intsertElemAtBegin(data);
53+
} else {
54+
if (length >= this.capacity) {
55+
//删除尾结点
56+
deleteElemAtEnd();
57+
}
58+
intsertElemAtBegin(data);
59+
}
60+
}
61+
62+
/**
63+
* 删除preNode结点下一个元素
64+
*
65+
* @param preNode
66+
*/
67+
private void deleteElemOptim(SNode preNode) {
68+
SNode temp = preNode.getNext();
69+
preNode.setNext(temp.getNext());
70+
temp = null;
71+
length--;
72+
}
73+
74+
/**
75+
* 链表头部插入节点
76+
*
77+
* @param data
78+
*/
79+
private void intsertElemAtBegin(T data) {
80+
SNode next = headNode.getNext();
81+
headNode.setNext(new SNode(data, next));
82+
length++;
83+
}
84+
85+
/**
86+
* 获取查找到元素的前一个结点
87+
*
88+
* @param data
89+
* @return
90+
*/
91+
private SNode findPreNode(T data) {
92+
SNode node = headNode;
93+
while (node.getNext() != null) {
94+
if (data.equals(node.getNext().getElement())) {
95+
return node;
96+
}
97+
node = node.getNext();
98+
}
99+
return null;
100+
}
101+
102+
/**
103+
* 删除尾结点
104+
*/
105+
private void deleteElemAtEnd() {
106+
SNode ptr = headNode;
107+
// 空链表直接返回
108+
if (ptr.getNext() == null) {
109+
return;
110+
}
111+
112+
// 倒数第二个结点
113+
while (ptr.getNext().getNext() != null) {
114+
ptr = ptr.getNext();
115+
}
116+
117+
SNode tmp = ptr.getNext();
118+
ptr.setNext(null);
119+
tmp = null;
120+
length--;
121+
}
122+
123+
private void printAll() {
124+
SNode node = headNode.getNext();
125+
while (node != null) {
126+
System.out.print(node.getElement() + ",");
127+
node = node.getNext();
128+
}
129+
System.out.println();
130+
}
131+
132+
public class SNode<T> {
133+
134+
private T element;
135+
136+
private SNode next;
137+
138+
public SNode(T element) {
139+
this.element = element;
140+
}
141+
142+
public SNode(T element, SNode next) {
143+
this.element = element;
144+
this.next = next;
145+
}
146+
147+
public SNode() {
148+
this.next = null;
149+
}
150+
151+
public T getElement() {
152+
return element;
153+
}
154+
155+
public void setElement(T element) {
156+
this.element = element;
157+
}
158+
159+
public SNode getNext() {
160+
return next;
161+
}
162+
163+
public void setNext(SNode next) {
164+
this.next = next;
165+
}
166+
}
167+
168+
public static void main(String[] args) {
169+
LRUBaseLinkedList list = new LRUBaseLinkedList();
170+
Scanner sc = new Scanner(System.in);
171+
while (true) {
172+
list.add(sc.nextInt());
173+
list.printAll();
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)