我将如何返回小于给定键的键数?我只是不知道从哪里开始。我有基础开始,但除此之外我不知道从哪里开始
public class LinkedListST<Key extends Comparable<Key>, Value> {
private Node first; // the linked list of key-value pairs
// a helper linked list data type
private class Node {
private Key key;
private Value val;
private Node next;
public Node(Key key, Value val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
}
public int rank (Key key) {
if(key == null) return 0;
//TODO
}
编辑:这是我到目前为止所拥有的,但我的 for 循环是错误的并且给了我错误
public int rank (Key key) {
int count = 0;
for(Node x = first; x != null; x = x.next){
if(x.next < key){
count++;
}
return count;
}
}