简体   繁体   English

删除 java 中 LinkedList 中的节点

[英]Deleting Node in LinkedList in java

I need to delete a Node from a linked list in java given an int that represents the index to be deleted.我需要从 java 中的链表中删除一个节点,给定一个表示要删除的索引的 int。 The head and tail of the list are defined outside of the method, they are defined in the class.列表的头部和尾部是在方法之外定义的,它们在 class 中定义。 I have defined the next node, and the data inside the node in another public class and have getters and setters for them.我已经定义了下一个节点,以及另一个公共 class 中的节点内的数据,并为它们设置了 getter 和 setter。 I need to return the data that was found in the node to be deleted and then adjust the head, tail and size (a variable defined in the class) accordingly.我需要返回在要删除的节点中找到的数据,然后相应地调整头部、尾部和大小(类中定义的变量)。

public class myExample<T> {
    private Node<T> head;
    private Node<T> tail;
    private int size;

    public T removeAtIndex(int index) {
        if (index < 1 | (index + 1) > size) {
            throw new IllegalArgumentException("Index out of bounds");
        }
        Node tempNode = head;
        if (index == 0) {
            head = tempNode.getNext();
        }
        for (int i = 0; tempNode != null && i < index - 1; i++) {
            tempNode.setNext(tempNode);
        }
        return (T) tempNode.getNext().getNext();
        tempNode.setNext(tempNode.getNext().getNext());
        tempNode.setNext(tempNode.getNext());
    }
}

Instead of providing a direct answer to that question, I shall suggest a general methodology to target those kinds of questions.我不会直接回答这个问题,而是建议一种针对这类问题的通用方法。

To target those questions, first of all, you need to design the algorithm.要针对这些问题,首先,您需要设计算法。 As I see, you already figured out to have a temporary variable and let it traverse the linkedlist.如我所见,您已经想出了一个临时变量并让它遍历链表。 However, it does not seem to be mature enough yet.但是,它似乎还不够成熟。

Secondly, you need to think about data encapsulations and responsibilities of the classes.其次,您需要考虑类的数据封装和职责。 I see that your myexample class holds three fields: head , tail , and size .我看到您的myexample class 包含三个字段: headtailsize So, what is the responsibility of myexample ?那么, myexample的责任是什么? Is that a linkedlist wrapper implementation?那是一个linkedlist包装器实现吗? If so, it needs to have other appropriate methods.如果是这样,它需要有其他适当的方法。 You need to decide the roles and responsibilities of the classes.您需要确定类的角色和职责。

By the way, having a tail resembles that the implementation is a Doubly-linked-list .顺便说一句,有一个tail类似于实现是一个Doubly-linked-list However, your code does not have anything regarding pre links.但是,您的代码没有任何关于pre链接的内容。 It seems you need to fix that also.看来你也需要解决这个问题。

Then, after you have something sketched up, you can start coding.然后,在你画好草图之后,你就可以开始编码了。 Your first task in that implementation phase is, convert your thoughts to compilable programming language statements.您在该实施阶段的第一个任务是将您的想法转换为可编译的编程语言语句。 So, type your code, and when you are done, compile it.所以,输入你的代码,完成后,编译它。 Your uploaded code does not compile.您上传的代码无法编译。 You need to fix that.你需要解决这个问题。

It is also a good idea to have a test case, so that you can run and debug your code.拥有一个测试用例也是一个好主意,这样您就可以运行和调试您的代码。 You need to think about and design the details of the test case.您需要考虑和设计测试用例的细节。 In your example, the test code, should create a linked list and call removeAtIndex .在您的示例中,测试代码应创建一个链表并调用removeAtIndex

Consider that piece of code:考虑那段代码:

    for (int i = 0; tempNode != null && i < index - 1; i++) {
        tempNode.setNext(tempNode);
    }

Let's trace that.让我们追踪它。 Prior to the loop, tempNode is assigned to something (actually head ).在循环之前, tempNode被分配给某些东西(实际上是head )。 The loop iterates, and the next pointer of tempNode is assigned to itself (thus losing the links to the rest of the linked list).循环迭代,将 tempNode 的下一个指针分配给它自己(从而丢失到链表的 rest 的链接)。 You do not update tempNode in the loop, so the code does that index-1 number of times.您不会在循环中更新tempNode ,因此代码会执行index-1的次数。 The loop breaks the links in the first iteration, and then does nothing in the following iterations.循环在第一次迭代中断开链接,然后在接下来的迭代中什么也不做。 It seems there is a bug in that loop, and I think you have noticed what the bug is, and how to fix it.该循环中似乎存在错误,我想您已经注意到错误是什么以及如何修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM