我已经创建了这个 Node 类,我想找到具有最大键的节点并返回它:
class Node{
int key;
Node next;
Node(int x){ key = x; next = null;
}
int max = 0;
Node findmax(Node h){
if(h==null) return null;
int max = 0;
Node t= null;
for(t=h; t!=null; t=t.next){
if(t.next.key>t.key) max=t.next.key;
t=t.next;
}
return t;
}
public static void main(String[] args){
Node a = new Node(0);
Node b = new Node(5);
Node c = new Node(12);
Node d = new Node(-12);
Node e = new Node(124);
Node f = new Node(2321);
Node g = new Node(-231);
findmax(a);
}
}
知道为什么我不断收到此编译错误:
Node.java:34:错误:无法从静态上下文引用非静态方法 findmax(Node) findmax(a);
最佳答案
public Node findNode()
{
Node node = nodeList.getHead(); // get the head of the list
Node prev = null; // stores the previous node
while(node != null)
{
if(prev != null && prev.getKey() > node.getKey())
{
Node maxNode = prev;
}
// store node as prev and get the next node
prev = node;
node = node.getNext();
}
return maxNode;
}
关于java - 单链表最大键搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015038/