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 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 = null;
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 ;
}
}
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);
IntNode cur = head;
while(cur!= null){
System.out.println(cur.data);
cur= cur.link;
}
IntNode node = new IntNode();
System.out.println(node.listLength(head));
if(node.find(head, 20))
System.out.println("found");
else
System.out.println("not found");
}
}
对于LinkNode长度,我建议沿以下
递归函数public int listLength(IntNode head, int count){
if(head.getLink() == null)
return count;
else
return listLength(head.getLink(), count+1);
}
对于这样的函数,呼叫列表listlength(head,1),其中头是一个节点将返回长度1,而call listLength(head,1)在带有两个节点的链接列表上返回2,将返回2,等等。
对于此链接列表中的特定值,我再次建议沿以下
提出递归函数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);
}
希望这会有所帮助!P.S.请更好地格式化您的问题