Java - 从链接列表中删除节点

时间:2013-12-07 06:35:48

标签: java

好的,这只是一个简单的程序,它将接收用户的输入并将其添加到linked list,并为他们提供view the listdelete a node的选项。它编译得很好,可以添加节点并显示列表,但不会删除节点。当我在没有键盘输入的情况下手动编码它时,即使使用相同的变量名称也可以解决问题。

    public class LinkedList {

       public class Link {

      public String content;
      public Link next;

      public Link(String content) {
         this.content = content;
      }

      public void display(){
         System.out.println(content);
      }
}

public static Link head;

LinkedList(){
    head = null;
}

public boolean isEmpty() {

    return(head == null);
}

public void insertFirstLink(String content) {
    Link newLink = new Link(content);

    newLink.next = head;

    head = newLink;
}

public void display() {
    Link theLink = head;

    while(theLink != null) {
        theLink.display();
        theLink = theLink.next;
    }
}

public Link removeLink(String content) {
    Link curr = head;
    Link prev = head;

    while(curr.content != content) {

        if (curr.next == null) {
        return null;
    }

        else {
            prev = curr;
            curr = curr.next;
        }
}

    if(curr == head) {

        head = head.next;


    }
    else {
        prev.next = curr.next;
    }
    return curr;
    }
    }


    public class Testlist {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
        int choice = 0;
        String content;
        System.out.println("Enter 1 to add to list");
        System.out.println("Enter 2 to display list");
        System.out.println("Enter 3 to delete node");
        System.out.println("Enter 4 to quit");
        choice = keyboard.nextInt();
        LinkedList newlist = new LinkedList();
        while(choice != 4) {

            if (choice == 1) {

                content = keyboard.next();
                newlist.insertFirstLink(content);
                newlist.display();


            }

            if (choice == 2) {

                newlist.display();
            }

            if (choice == 3) {


                content = keyboard.next();  // this is where is goes wrong
                newlist.removeLink(content);
                newlist.display();
            }


            System.out.println("Enter 1 to add to list");
            System.out.println("Enter 2 to display list");
            System.out.println("Enter 3 to delete node");
            System.out.println("Enter 4 to quit");
            choice = keyboard.nextInt();

        }
        } 

    }

4 个答案:

答案 0 :(得分:2)

您正在使用!=,它通过对象的引用进行比较,而不是按值进行比较。您想使用.equals(),即:

while(!curr.content.equals(content))

答案 1 :(得分:0)

有些人可能需要仔细检查,但我很确定这是因为nextInt()方法抓取了第一个整数,就是全部。它在输入流中留下'enter / carriage return'。因此,当运行next()方法时,它会抓取进入。明确地放入一些调试行来查看内容是什么。

答案 2 :(得分:0)

在此行中也使用'equals''if(curr == head) {'.

答案 3 :(得分:0)

对于比较字符串,您应该使用equals或equalsignorecase()

例如String1 =“xyz”; 和String2 =“xyz”这两个字符串是不同的,如果你使用==或!=来对它们进行比较,因为比较对象而不是实际内容。您的程序的正确实现将是

package stackoverflow.practice;

public class LinkedList {

public class Link {

public String content;
public Link next;

public Link(String content) {



this.content = content;
}

public void display(){
System.out.println(content);
}


}

public static Link head;

LinkedList(){
head = null;
}

public boolean isEmpty() {

return(head == null);
}

public void insertFirstLink(String content) {
Link newLink = new Link(content);

newLink.next = head;

head = newLink;
}

public void display() {
Link theLink = head;

while(theLink != null) {
    theLink.display();
    theLink = theLink.next;
}
}

public Link removeLink(String content) {
Link curr = head;
Link prev = head;

while(!curr.content.equalsIgnoreCase(content)) {

    if (curr.next == null) {
    return null;
}

    else {
        prev = curr;
        curr = curr.next;
    }
}

if(curr == head) {

    head = head.next;


}
else {
    prev.next = curr.next;
}
return curr;
}
}