【发布时间】:2022-01-20 01:52:30
【问题描述】:
我认为正确插入值也可以在控制台屏幕上查看,但是搜索方法无法以奇怪的方式工作
我的所有代码都缺少什么?它非常清晰和基本的结构
在输出部分“43”通常包括结构,它需要显示为真,但可能它不起作用我不知道原因。 输出: 43 43 33 43 13 3 检查:假
NodeList head;
public void insert(int insertKey) {
NodeList myhead = new NodeList();
if (head == null) {
myhead.setData(insertKey);
} else {
myhead.setData(insertKey);
myhead.setLink(head);
}
head = myhead;
}
public boolean search(int key) {
boolean check = false;
NodeList current = head;
while (current != null) {
if (current.getData() == key) {
check = true;
break;
}
current = current.getLink();
}
return check;
}
public void display() {
if (head == null) {
} else {
while (head != null) {
System.out.print(head.getData() + " ");
head = head.getLink();
}
System.out.println();
}
}
package ds_project;
public class NodeList {
private NodeList Link;
private int data;
public NodeList(int data) {
this.Link = null;
this.data = data;
}
public NodeList() {
}
public NodeList getLink() {
return Link;
}
public void setLink(NodeList Link) {
this.Link = Link;
}
public int getData() {
return this.data;
}
public void setData(int data) {
this.data = data;
}
result screen
SLL Linkedlist = new SLL();
Linkedlist.insert(3);
Linkedlist.insert(13);
Linkedlist.insert(43);
Linkedlist.insert(33);
Linkedlist.insert(43);
Linkedlist.insert(43);
Linkedlist.display();
boolean check=Linkedlist.search(43);
System.out.println("check: " + check);}
输出: 43 43 33 43 13 3 检查:假
【问题讨论】:
标签: java data-structures linked-list