如何使用java将排序节点插入到通用链表中
在比较一般类型数据时,我遇到了一个问题,在该数据中,与方法进行比较以进行比较,例如,我使用它来查找当前和以前的节点,其中将在它们之前或之后插入新节点,然后我
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Node 类需要正确实现
可比较
界面。 Node 类中的compareTo()
方法应如下所示:或者以其他方式进行定义,使得如果
this.data
位于other.data< 之前/code> 按照预期的顺序,则该函数返回负值。
编辑:
compareTo()
来自 Comparable 接口,不是Comparator
接口,使用compare()
Your
Node
class needs to properly implement theComparable
interface. YourcompareTo()
method inside of the Node class should then look something like this:Or otherwise be defined such that if
this.data
comes beforeother.data
in the intended ordering then the function returns a negative value.Edit:
compareTo()
is from the Comparable interface, not theComparator
interface which usescompare()