File tree Expand file tree Collapse file tree 1 file changed +5
-5
lines changed
src/main/java/com/algorithm/study/demo/LRUCache Expand file tree Collapse file tree 1 file changed +5
-5
lines changed Original file line number Diff line number Diff line change 10
10
* @Version: 1.0
11
11
*/
12
12
public class LRULinked <K ,V >{
13
+ //缓存
13
14
private final Map <K , V > cacheMap = new HashMap <>();
14
-
15
+ //根节点
15
16
private Node <K , V > root ;
16
17
private int cacheSize ;
17
18
private int size ;
@@ -29,23 +30,22 @@ public void put(K key,V value){
29
30
Node <K , V > temp =root .next ;
30
31
if (temp ==null ){
31
32
root =null ;
32
- size --;
33
33
}else {
34
34
Node <K , V > current =root ;
35
35
while (temp .next !=null ){
36
36
current =temp ;
37
37
temp =temp .next ;
38
38
}
39
39
current .next =null ;
40
- size --;
41
40
}
41
+ size --;
42
42
}
43
43
node .next =root ;
44
44
root =node ;
45
45
size ++;
46
46
}
47
47
public V get (K key ){
48
- for (Node <K ,V > node = root ; node !=null ; node =node .next ){
48
+ for (Node <K ,V > node = root ; node !=null &&! root . key . equals ( key ) ; node =node .next ){
49
49
if (node .next .key .equals (key )){
50
50
Node <K , V > nodeNew =new Node <K , V >(node .next .key ,node .next .value );
51
51
node .next =node .next .next ;
@@ -84,7 +84,7 @@ public static void main(String[] args) {
84
84
linked .put ("a" ,"a" );
85
85
linked .put ("b" ,"b" );
86
86
linked .put ("c" ,"c" );
87
- linked .get ("a " );
87
+ linked .get ("b " );
88
88
linked .put ("d" ,"d" );
89
89
System .out .println (linked .size );
90
90
System .out .println (linked .toString ());
You can’t perform that action at this time.
0 commit comments