如何使用java将排序节点插入到通用链表中

发布于 2025-01-19 08:52:34 字数 1417 浏览 1 评论 0原文

在比较一般类型数据时,我遇到了一个问题,在该数据中,与方法进行比较以进行比较,例如,我使用它来查找当前和以前的节点,其中将在它们之前或之后插入新节点,然后我

for(prev =null,current = head; current !=null&& newNode.getData().compareTo(current.getData)<0;prev =current,current =current.getNext());

和I尝试在节点类中实现可比性,但我无法找到一种使其正常工作的方法,因为它不能定义大于操作,而不是操作。 `linkedlists1&lt; t&gt; { node&lt; t&gt;头;

public LinkedLists1() {
    this.head = null;
}

public LinkedLists1(T data) {
    Node<T> temp = new Node<T>(data);
    head = temp;
}

public void insert(T data) {
    Node<T> newNode = new Node<T>(data);
    if (head == null) {
        head = newNode;
    } else {
        Node<T> prev = null;
        Node<T> current = new Node<T>(head.getData());
        implement this for loop
        for (prev = null, current = head; current != null
                && newNode.compareTo(current) < 0; prev = current, current = current.getNext());
        if (current != null) {
            if (head == current) {
                newNode.setNext(head);
                head = newNode;
            } else if (current.getNext() == null) {
                current.setNext(newNode);
            } else {
                newNode.setNext(current.getNext());
                current.setNext(newNode);
            }
        }
    }
}

}`

因此,我尝试使用与方法进行比较,但是正如我所说的那样,它不是定义的,也不是我得到的。

I have encountered a problem while comparing the generic type data in which the compare to method isn't declared for comparing for example I used this to find the current and previous nodes in which the new node will be inserted between before or after them

for(prev =null,current = head; current !=null&& newNode.getData().compareTo(current.getData)<0;prev =current,current =current.getNext());

And I tried implementing comparable inside the Node class but I couldn't figure out a way to make it work since it doesn't define the greater than and less than operations.
`public class LinkedLists1<T> {
Node<T> head;

public LinkedLists1() {
    this.head = null;
}

public LinkedLists1(T data) {
    Node<T> temp = new Node<T>(data);
    head = temp;
}

public void insert(T data) {
    Node<T> newNode = new Node<T>(data);
    if (head == null) {
        head = newNode;
    } else {
        Node<T> prev = null;
        Node<T> current = new Node<T>(head.getData());
        implement this for loop
        for (prev = null, current = head; current != null
                && newNode.compareTo(current) < 0; prev = current, current = current.getNext());
        if (current != null) {
            if (head == current) {
                newNode.setNext(head);
                head = newNode;
            } else if (current.getNext() == null) {
                current.setNext(newNode);
            } else {
                newNode.setNext(current.getNext());
                current.setNext(newNode);
            }
        }
    }
}

}`

So I tried using the compare to method but as I said it's not defined or that's what I got.

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

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

发布评论

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

评论(1

゛时过境迁 2025-01-26 08:52:34

您的 Node 类需要正确实现 可比较界面。 Node 类中的 compareTo() 方法应如下所示:

public int compareTo(Node other) {
    return this.data - other.data;
}

或者以其他方式进行定义,使得如果 this.data 位于 other.data< 之前/code> 按照预期的顺序,则该函数返回负值。

编辑: compareTo() 来自 Comparable 接口,不是 Comparator 接口,使用 compare()

Your Node class needs to properly implement the Comparable interface. Your compareTo() method inside of the Node class should then look something like this:

public int compareTo(Node other) {
    return this.data - other.data;
}

Or otherwise be defined such that if this.data comes before other.data in the intended ordering then the function returns a negative value.

Edit: compareTo() is from the Comparable interface, not the Comparator interface which uses compare()

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