如何从链表中删除节点

发布于 2024-12-09 00:31:21 字数 3509 浏览 0 评论 0原文

嘿伙计们,我写了这个deleteNode()方法,如果我使用数字(int),它可以工作,但当我尝试传递一个字符串时,它就不行。我正在打印一个 String[] 名称列表,并且我正在尝试删除名单上的某个名字。当我输入名称时,它会打印“未找到节点”。就像我说的,如果我打印出一个数字列表,它效果很好,但如果我改变并打印一个字符串,它就不行了。任何帮助表示赞赏。

   public class BigNode {


   public String dataitems; 
    public BigNode next; 
    BigNode front ;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String number){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(number);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(number);
        }
    }
    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.next = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){


        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

    public void showList(BigNode front){
        BigNode current;
        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
    }
    public static void main(String[] args) {
                   String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"}; 

        BigNode x = new BigNode(); 
                   x.printNodes(names); 
                   Scanner in = new Scanner(System.in);
                   String delete = in.next();
                  x.deleteNode(x.front, delete); 
          }

String[] names = {name1, name2, name3, name4}

- 首先打印列表,然后询问要删除哪个名称。

Hey guys I wrote this deleteNode() method that works if I used numbers(int) but doesn't when I try to pass a string in. I'm printing out a String[] list of names and I'm trying to delete a certain name off the list. When I enter a name, it prints "Node not found". Like I said, if I print out a list of numbers it works great but if I change up and print a string it doesnt. Any help is appreciated.

   public class BigNode {


   public String dataitems; 
    public BigNode next; 
    BigNode front ;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String number){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(number);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(number);
        }
    }
    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.next = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){


        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

    public void showList(BigNode front){
        BigNode current;
        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
    }
    public static void main(String[] args) {
                   String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"}; 

        BigNode x = new BigNode(); 
                   x.printNodes(names); 
                   Scanner in = new Scanner(System.in);
                   String delete = in.next();
                  x.deleteNode(x.front, delete); 
          }

String[] names = {name1, name2, name3, name4}

-First it prints the list, then ask what name to delete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热血少△年 2024-12-16 00:31:21

编辑:好的,我已经发现您发布的示例代码有什么问题。

您正在调用 Scanner.next() 来读取单个单词。所有节点值都是两个单词。因此,如果我输入“Sally Mae”,它实际上只是在寻找“Sally”。

这与 BigNode 中的大部分代码无关(尽管这当然可以变得更优雅)。 这样的:

String delete = in.next();

基本上是

String delete = in.nextLine();

现在我强烈建议您不要只是更改代码,而是考虑一下您可以自己诊断此问题的方法:

  • 添加日志记录到您的代码中显示您正在寻找的值,以及您测试时的每个值
  • 使用调试器单步调试代码,观察变量
  • 使用单元测试来测试代码 - 这些不会向您显示问题(因为它不是不在你通常编写测试的代码中)但他们会给出您更有信心问题不在测试代码中。

如果您尝试一些或最好所有这些方法,您将从这个问题中学到的东西不仅仅是如何使用扫描仪...


在不同的地方,您使用 == 运算符比较字符串引用。只有当您传入对列表中存在的实际字符串对象之一的引用时,才会找到您的节点 - 而不是对 equal 字符串对象的引用。

您想要类似的东西:(

if (curr.dataitems.equals(value))

但要仔细检查 null)。

EDIT: Okay, I've found out what's wrong with the sample code you've posted.

You're calling Scanner.next() which reads a single word. All of your node values are two words. So if I type in "Sally Mae" it's actually just looking for "Sally".

This has nothing to do with the majority of the code in BigNode (although that could certainly be made more elegant). Basically this:

String delete = in.next();

should be

String delete = in.nextLine();

Now I would strongly suggest that you don't just change the code, but instead think about the ways you could have diagnosed this for yourself:

  • Add logging to your code to show value you were looking for, and each value as you tested it
  • Use a debugger to step through the code, watching variables
  • Use unit tests to test the code - those wouldn't have shown you the problem (as it wasn't in code you'd usually write tests for) but they would have given you greater confidence that the problem wasn't in the tested code.

If you try some or preferrably all of those approaches, you'll have learned a lot more from this question than just how to use Scanner...


In various places, you're comparing string references by using the == operator. That will only find your node if you pass in a reference to one of the actual string objects which exists in the list - not a reference to an equal string object.

You want something like:

if (curr.dataitems.equals(value))

(but with careful null checking).

你丑哭了我 2024-12-16 00:31:21

您应该使用 String.equals 比较而不是 == 比较。

if(curr.dataitems == value) {

应该是:

if(curr.dataitems.equals(value) {

You should be using String.equals comparison instead of == comparison,.

if(curr.dataitems == value) {

should be:

if(curr.dataitems.equals(value) {
树深时见影 2024-12-16 00:31:21

您可以与 == 进行比较。对于int,它比较值,但对于String,仅比较引用。因此,您想删除具有相同值但在不同对象中的字符串,这将失败。

请改用 String.equals() 。

You do comparisons with ==. For int this compares the values, but for String only the references. So is you want to remove a String with the same value but in different objects this will fail.

Use String.equals() instead.

老街孤人 2024-12-16 00:31:21

您应该始终使用 equals() 来比较对象值,而不是 == 。例如,在您的代码中,这一行:

curr.dataitems == value

应该写为:

curr.dataitems.equals(value)

You should always use equals() for comparing object values, not == . For instance, in your code this line:

curr.dataitems == value

should have been written as:

curr.dataitems.equals(value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文