[英]Add Method in Sorted Doubly Linked List - java
我真的很难为我的已排序的双向链表创建add方法。 我试图将对象添加到列表中,以可比的顺序为基础。 我目前的代码:
public void add(DistanceEvent obj){
DistanceEvent newNode = obj;
if (firstLink == null)
{
firstLink = newNode;
return;
}
else if (obj.compareTo(firstLink) < 0) {
newNode.next = firstLink;
firstLink = newNode;
}
else
{DistanceEvent after = firstLink.next;
DistanceEvent before = firstLink;
while (after != null){
if (obj.compareTo(after) < 0)
break;
before = after;
after = after.next;
}
newNode.next =before.next;
before.next = newNode;
}
}
}
我试图将10个对象添加到此列表,但是当我显示列表的内容时,我只返回两个对象。 我已经为此苦苦挣扎了一段时间,对Java来说我还是一个新手,所以可以提供任何帮助。 这是我的链表类的其余部分:
public class DoubleEndedLinkedList {
DistanceEvent firstLink;
DistanceEvent lastLink;
public void insertInFirstPosition(String name, String country, int turn){
DistanceEvent theNewLink = new DistanceEvent(name, country, turn);
if(isEmpty()){
lastLink = theNewLink;
} else {
firstLink.previous = theNewLink;
}
theNewLink.next = firstLink;
firstLink = theNewLink;
}
public void insertInLastPosition(String name, String country, int turn){
DistanceEvent theNewLink = new DistanceEvent(name, country, turn);
if(isEmpty()){
firstLink = theNewLink;
} else {
lastLink.next = theNewLink;
theNewLink.previous = lastLink;
}
lastLink = theNewLink;
}
public boolean isEmpty(){
return(firstLink == null); }
public void display() {
DistanceEvent theLink = firstLink;
while(theLink != null){
theLink.display();
theLink = theLink.next;
System.out.println();
}
}
public boolean insertAfterKey(String name, String country, int turn, int key){
DistanceEvent theNewLink = new DistanceEvent(name, country, turn);
DistanceEvent currentDistanceEvent = firstLink;
while (currentDistanceEvent.turn != key){
currentDistanceEvent = currentDistanceEvent.next;
if(currentDistanceEvent == null){
return false;
}
}
if(currentDistanceEvent == lastLink){
theNewLink.next = null;
lastLink = theNewLink;
} else {
theNewLink.next = currentDistanceEvent.next;
currentDistanceEvent.next.previous = theNewLink;
}
theNewLink.previous = currentDistanceEvent;
currentDistanceEvent.next = theNewLink;
return true;
}
public void add(DistanceEvent obj){
DistanceEvent newNode = obj;
if (firstLink == null)
{
firstLink = newNode;
return;
}
else if (obj.compareTo(firstLink) < 0) {
newNode.next = firstLink;
firstLink = newNode;
}
else
{DistanceEvent after = firstLink.next;
DistanceEvent before = firstLink;
while (after != null){
if (obj.compareTo(after) < 0)
break;
before = after;
after = after.next;
}
newNode.next =before.next;
before.next = newNode;
}
}
}
对于代码的可读性,我深表歉意。 谢谢你的时间!
我已经为此苦苦挣扎了一段时间,对Java来说我还是一个新手,所以可以提供任何帮助。
好吧,我确定有人可以为您找到代码中的错误。 但是,从长远来看,我认为这对鼓励您提高调试技能会有所帮助。
这里有一些建议:
阅读有关如何使用其调试器的IDE教程材料。 了解如何设置断点,单步执行,查看变量的值,查看对象内部等。
尝试在精心选择的位置向代码中添加一些“跟踪打印”语句,以了解其工作情况。
学习阅读您自己的代码并可视化它在做什么。 对初学者有用的一种技术是“手动执行”代码。 用铅笔,橡皮擦取一张纸,写出所有变量,然后将对象绘制为带有包含文字值和对其他对象的引用的框的框(如箭头所示)。 然后通过单步执行一个代码语句,更新变量和字段来“执行”程序。
经过一些练习,您将可以在脑海中完成所有任务...
我只是从头开始讨论此主题,但是如果您在Google上搜索“如何调试Java”,那么这里有很多很好的资源:教程,视频等。
最后,一些技巧:
有条理。 了解代码的实际含义。 查看您拥有的证据。
不要依靠“预感”。 如果您对发生的事情做出的假设不是基于证据的,则很容易浪费时间寻找幻像。
假设有99%的时间,该错误将在您的代码中。 如果仅使用标准Java类库,则占99.99%。
调试多线程程序很难.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.