Home > Back-end >  Who can help me have a look at the LRU algorithm which is wrong, and I always in cattle guest above
Who can help me have a look at the LRU algorithm which is wrong, and I always in cattle guest above

Time:03-05

 
Package cn. Yangyt. Lru;

Import the Java. Util. HashMap;
import java.util.Scanner;

Public class Main {

Private final HashMap The map=new HashMap<> (a);
Private final Node head=new Node ();
Private final Node tail=new Node ();
Private final int capacity;
private int size;

Public Main (int capacity) {
Head. Next=tail;
Tail. The pre=head;
This. Capacity=capacity;
Size=0;
}

Static class Node {
Private int key;
Private int value.
Private Node pre.
Private Node next;

Public Node () {
}

Public Node (int key, int value) {
This. The key=key;
This value=https://bbs.csdn.net/topics/value;
}

}

//two-way link header method,
Private void invoke the (Node to Node) {
The Node originHead=head. The next;
Head. Next=node;
Node. The pre=head;
Node. Next=originHead;
OriginHead. Pre=node;
}

//two-way linked list to delete a node
Private void remove (Node to Node) {
The Node next=Node. The next;
The Node pre=Node. The pre;
The pre. Next=next;
Next, the pre=pre;
Node. The pre=null;
Node. Next=null;
}

Public void put (int key, int value) {
The Node Node=map. Get (key);
If (node==null) {
If (size & lt; Capacity) {
size++;
} else {
//delete list end node
The Node pre=tail. The pre;
Map. Remove (pre. Key);
Remove (pre);
}
The Node node1=new Node (key, value);
Invoke the (node1);
The map. The put (key, node1);
} else {
//will list the key value change
Node. The value=https://bbs.csdn.net/topics/value;
The map. The put (key, node);
//the node value=https://bbs.csdn.net/topics/value;
//remove (node);
//invoke the (node);
}
}

Public int the get (int key) {
The Node Node=map. Get (key);
If (node==null) {
return -1;
}
Remove (node);
Invoke the (node);
The return node. The value;
}

Public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
The Main lru=new Main (sc) nextInt ());
While (sc) hasNextLine ()) {
String STR=sc. NextLine ();
String [] c=STR. The split (" ");
String m=c [0];
If (" p ". The equals (m)) {
Lru. Put (Integer. ParseInt (c [1]), an Integer. The parseInt (c [2]));
} else if (" g ". The equals (m)) {
System. The out. Println (lru) get (Integer) parseInt (c [1])));
}
}
Sc. The close ();
}
}



Has been submitted to the
Returns a non-zero
Please check whether illegal access, such as an array

CodePudding user response:

The
reply: refer to the original poster Never compromise
 
Has been submitted to the
Returns a non-zero
Please check whether illegal access conditions, such as an array


Add a loop zero judgment conditions, or has been dead circulation,

[code=Java]
Public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
Test10 lru=new Test10 (sc) nextInt ());
While (sc) hasNextLine ()) {
String STR=sc. NextLine ();
if (STR) equals (" 0 ")) break;
String [] c=STR. The split (" ");
String m=c [0];
If (" p ". The equals (m)) {
Lru. Put (Integer. ParseInt (c [1]), an Integer. The parseInt (c [2]));
} else if (" g ". The equals (m)) {
System. The out. Println (lru) get (Integer) parseInt (c [1])));
}
}
Sc. The close ();
}
  • Related