Java i';我的链接列表有问题我可以显示列表的内容,但我找不到节点或列表的长度
对于linkNode长度,我建议使用递归函数,如下所示Java i';我的链接列表有问题我可以显示列表的内容,但我找不到节点或列表的长度,java,linked-list,Java,Linked List,对于linkNode长度,我建议使用递归函数,如下所示 public class IntNode { int data; IntNode link; IntNode() { this.link = null; } public IntNode(int initialData, IntNode initialLink) { data = initialData; link = initialLink
public class IntNode {
int data;
IntNode link;
IntNode() {
this.link = null;
}
public IntNode(int initialData, IntNode initialLink) {
data = initialData;
link = initialLink;
}
public void addNode(int element) {
link = new IntNode(element, link);
}
public int getData() {
return data;
}
public IntNode getLink() {
return link;
}
public void removeNodeAfter()
{
link = link.link;
}
public void setLink(IntNode newLink) {
link = newLink;
}
void insertAtHead(int input) {
input = data;
}
//this method doesnt work
public int listLength(IntNode head) {
IntNode cursor = 0;
int ans;
ans = 0;
for (cursor = head; cursor != null; cursor = cursor.link)
ans++;
return ans;
}
//this method doesnt work either
public boolean find(IntNode head, int searchKey) {
if (head == null) {
return false;
}
IntNode current = head;
while (current != null) {
if (current.data == searchKey) {
return true;
}
current = current.link;
}
return false;
}
import java.util.Scanner;
public class linkedList1 {
public static void main(String[] args) {
IntNode head;
IntNode tail;
head = new IntNode(10, null);
head = new IntNode(20, head);
head = new IntNode(25, head);
head = new IntNode(30, head);
while (head != null) {
System.out.println(head.data);
head = head.link;
}
IntNode node = new IntNode();
System.out.println(node.listLength(null));
if (node.find(head, 20)) System.out.println("found");
else
System.out.println("not found");
}
}
public int listLength(IntNode head, int count){
if(head.getLink() == null)
return count;
else
return listLength(head.getLink(), count+1);
}
对于这样的函数,head为单个节点的调用listLength(head,1)将返回长度1,而具有两个节点的链表上的调用listLength(head,1)将返回2,以此类推
为了在这个链表中找到一个特定的值,我再次建议使用如下递归函数
public class IntNode {
int data;
IntNode link;
IntNode() {
this.link = null;
}
public IntNode(int initialData, IntNode initialLink) {
data = initialData;
link = initialLink;
}
public void addNode(int element) {
link = new IntNode(element, link);
}
public int getData() {
return data;
}
public IntNode getLink() {
return link;
}
public void removeNodeAfter()
{
link = link.link;
}
public void setLink(IntNode newLink) {
link = newLink;
}
void insertAtHead(int input) {
input = data;
}
//this method doesnt work
public int listLength(IntNode head) {
IntNode cursor = 0;
int ans;
ans = 0;
for (cursor = head; cursor != null; cursor = cursor.link)
ans++;
return ans;
}
//this method doesnt work either
public boolean find(IntNode head, int searchKey) {
if (head == null) {
return false;
}
IntNode current = head;
while (current != null) {
if (current.data == searchKey) {
return true;
}
current = current.link;
}
return false;
}
import java.util.Scanner;
public class linkedList1 {
public static void main(String[] args) {
IntNode head;
IntNode tail;
head = new IntNode(10, null);
head = new IntNode(20, head);
head = new IntNode(25, head);
head = new IntNode(30, head);
while (head != null) {
System.out.println(head.data);
head = head.link;
}
IntNode node = new IntNode();
System.out.println(node.listLength(null));
if (node.find(head, 20)) System.out.println("found");
else
System.out.println("not found");
}
}
public int listLength(IntNode head, int count){
if(head.getLink() == null)
return count;
else
return listLength(head.getLink(), count+1);
}
希望这有帮助!另外,请将问题的格式设置得更好
您的链接列表代码没有问题。问题在于你的主要方法。我已经改正了。我把它作为一种练习留给你去找出你做错了什么
public boolean find(IntNode head, int searchKey){
// Check if this is the correct node
if(head.getData() == searchKey)
return true;
// Check if this is the last node
else if(head.getLink() == null)
return false;
else
return find(head.getLink(), searchKey);
}
欢迎来到堆栈溢出!我想这段代码的95%与您的问题无关。请创建一个实例来演示您的问题。我理解@joe问题是什么?---------------配置:------30 25 20 10线程“main”java.lang.NullPointerException在IntNode.listLength(IntNode.java:57)在linkedList1.main(linkedList1.java:24)中出现异常过程已完成。我尝试使用您给我的代码,但我发现这个错误@Sishaar RaoYou需要提供更多上下文。抛出此错误的行是什么。也许调试是为了找出发生了什么,很可能您正在传入null,就像在System.out.println(node.listLength(null))中一样;我把它改为System.out.println(node.listLength(head,30));然后确保你的头不是空的。调试并查看是否正确构造了链表。