我需要编写一个方法来循环查看链表是否在链接列表中。任何帮助?Java搜索LinkedList以获取数据返回true/false?
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
for(somethinggoeshere..){
if(head==data){
return true;
}else{
return false;
}
}
任何帮助?
编辑:我的一个LinkedListNode类:
public class LinkedListNode {
private Object data;
private LinkedListNode next;
public LinkedListNode(Object data, LinkedListNode next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
编辑:对于那些有兴趣谁最终解决方案:
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
LinkedListNode temp = head;
while(temp!= null) // check if you have reached the tail
{
if(data.equals(temp.getData()))
{
return true;
}
temp = temp.getNext(); // move to the next node
} // end of while loop
return false;
} // end of find method
我不会建议调用类'LinkedList' ..你可能混淆了Java的'LinkedList'类 – Alejandro
你尝试过什么?你创建了LinkedListNode对象吗?为什么不能只看到LinkedListNode.NextNode == null? –
我将遍历的链表将包含数据。我这样做的全部原因是用一组字符(例如“ump”)搜索链接列表,并在列表中出现短语“ump”时返回。 – Cole