我创建了一个链表,以及一些方法。我的任务是创建一个名单。在main中,指示用户输入名称,然后程序查看名称是否位于列表中。它确实编译,但我唯一无法弄清楚的是如何打印找不到名称。谢谢您的帮助!这是我的代码
package linkedlists;
import java.util.*;
public class Link {
public String Name;
public Link next;
public Link(String Name){
this.Name = Name;
}
public void display(){
System.out.println("Name in List " + Name);
}
@Override
public String toString(){
return Name;
}
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
LinkList theLinkedList = new LinkList();
theLinkedList.insertFirstLink("James");
theLinkedList.insertFirstLink("John");
theLinkedList.insertFirstLink("Michael");
theLinkedList.insertFirstLink("Peter");
theLinkedList.insertFirstLink("Allison");
theLinkedList.insertFirstLink("Daniel");
theLinkedList.insertFirstLink("George");
theLinkedList.insertFirstLink("Simon");
theLinkedList.insertFirstLink("Jason");
theLinkedList.insertFirstLink("Mark");
System.out.print("Please enter a Name to search for");
System.out.println();
String name;
name = input.nextLine();
//System.out.println(theLinkedList.find(name).Name);
if (theLinkedList.find(name).Name == null){
System.out.println("Not Found");
System.out.println();
theLinkedList.display();
}
else {
System.out.println(theLinkedList.find(name).Name + " was found.");
System.out.println();
theLinkedList.display();
}
}
}
这是LinkList类 class LinkList { public Link firstLink;
LinkList(){
firstLink = null;
}
public boolean isEmpty(){
return (firstLink == null);
}
public void insertFirstLink(String Name){
Link newLink = new Link(Name);
newLink.next = firstLink;
firstLink = newLink;
}
public Link removeFirst(){
Link linkReference = firstLink;
if (!isEmpty()){
firstLink = firstLink.next;
}
else {
System.out.println("Empty Linked List");
}
return linkReference;
}
public void display(){
Link theLink = firstLink;
while (theLink != null){
theLink.display();
System.out.println("Next Name in List: " + theLink.next);
theLink = theLink.next;
System.out.println();
}
}
public Link find(String Name){
Link theLink = firstLink;
if(!isEmpty()){
while(!(theLink.Name.equals(Name))){
if(theLink.next == null){
return null;
} else {
theLink = theLink.next;
}
}
} else {
System.out.println("Empty LinkedList");
}
return theLink;
}
public Link removeLink(String Name){
Link currentLink = firstLink;
Link previousLink = firstLink;
while(currentLink.Name != Name){
if (currentLink.next == null){
return null;
}
else{
previousLink = currentLink;
currentLink = currentLink.next;
}
}
if (currentLink == firstLink){
firstLink = firstLink.next;
}
else {
System.out.println(" Found a match!");
System.out.println("Current Link: " + currentLink);
System.out.println("First Link: " + firstLink);
previousLink.next = currentLink.next;
}
return currentLink;
}
}
答案 0 :(得分:0)
遍历LinkedList的模式通常保持不变。
6
4
)代码
public Link find(String Name){
if(isEmpty()) {
System.out.println("Empty LinkedList");
return null;
}
Link theLink = firstLink;
while(theLink != null) {
if (theLink.Name.equals(Name)) {
return theLink;
}
theLink = theLink.next;
}
// looped through the whole list, didn't find anything
return null;
}
现在,这是一个迭代解决方案,但更先进的解决方案是以递归方式编写
public Link find(String Name) {
if(isEmpty()) {
System.out.println("Empty LinkedList");
return null;
}
return findRecur(firstLink, Name);
}
private Link findRecur(Link link, String Name){
if (link == null) return null;
if (link.Name.equals(Name)) return link;
return findRecur(link.next, Name);
}
无论哪种方式,请致电
final Link found = theLinkedList.find(name);
if (found == null){
System.out.println("Not Found");
} else {
System.out.println(found.Name + " was found.");
}
System.out.println();
theLinkedList.display();
答案 1 :(得分:0)
更换线 if(theLinkedList.find(name).Name == null){
与
if(null == theLinkedList.find(name)){
您的语句尝试获取与输入的名称相关的Link对象,但是当输入的名称不在列表中时,返回null而不是Link对象。只需检查查找函数中返回的值是否为null,以确保列表中不存在该名称即可。 您的代码尝试对名称不存在时返回的null Link对象进行操作 - 这将导致您必须看到的空指针异常。