链接列表输出

时间:2016-08-02 02:48:35

标签: java linked-list

我创建了一个链表,以及一些方法。我的任务是创建一个名单。在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;
    }

}

2 个答案:

答案 0 :(得分:0)

遍历LinkedList的模式通常保持不变。

  1. 检查列表是否为空 - >可以立即返回
  2. 存储对第一个链接的引用
  3. 虽然还有任何链接,但请继续,否则请转到6
  4. 做一些逻辑,当你遇到某种情况时回来
  5. 移动链接,(返回4
  6. 循环结束,返回函数的最终状态
  7. 代码

    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对象进行操作 - 这将导致您必须看到的空指针异常。