简体   繁体   English

排序的双链表中的添加方法-Java

[英]Add Method in Sorted Doubly Linked List - java

I am really struggling to create an add method for my sorted doubly linked list. 我真的很难为我的已排序的双向链表创建add方法。 I am trying to add an object to a list in order based on a comparable. 我试图将对象添加到列表中,以可比的顺序为基础。 My code at the moment: 我目前的代码:

       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;

       }
   }
}

I am trying to add 10 objects to this list, however i am only returning two when i display the contents of the list. 我试图将10个对象添加到此列表,但是当我显示列表的内容时,我只返回两个对象。 I have been struggling with this for a while and I'm very new to java so any help would be appreciated. 我已经为此苦苦挣扎了一段时间,对Java来说我还是一个新手,所以可以提供任何帮助。 Here is the rest of my linkedlist class: 这是我的链表类的其余部分:

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;

       }
   }
  }

I apologise for the readability of my code. 对于代码的可读性,我深表歉意。 Thanks for your time! 谢谢你的时间!

I have been struggling with this for a while and I'm very new to java so any help would be appreciated. 我已经为此苦苦挣扎了一段时间,对Java来说我还是一个新手,所以可以提供任何帮助。

Well, I'm sure that someone could find the bug or bugs in your code for you. 好吧,我确定有人可以为您找到代码中的错误。 But I think it would be more helpful to you (in the long run) to encourage you to improve your debugging skills. 但是,从长远来看,我认为这对鼓励您提高调试技能会有所帮助。

Here are a couple of suggestions: 这里有一些建议:

  • Read your IDE's tutorial material on how to use its debugger. 阅读有关如何使用其调试器的IDE教程材料。 Learn how to set breakpoints, single step, look at the values of variables, look inside objects, and so on. 了解如何设置断点,单步执行,查看变量的值,查看对象内部等。

  • Try adding some "trace print" statements to your code at carefully chosen places to get a handle on what it is doing. 尝试在精心选择的位置向代码中添加一些“跟踪打印”语句,以了解其工作情况。

  • Learn to read your own code and visualize what it is doing. 学习阅读您​​自己的代码并可视化它在做什么。 One technique that is useful for beginners is to "hand execute" the code. 对初学者有用的一种技术是“手动执行”代码。 Get a piece of paper a pencil and an eraser, write out all of the variables, and draw the objects as boxes with cells containing literal values and references to other objects drawn as arrows. 用铅笔,橡皮擦取一张纸,写出所有变量,然后将对象绘制为带有包含文字值和对其他对象的引用的框的框(如箭头所示)。 Then "execute" the program by stepping through code one statement at a time, updating the variables and fields. 然后通过单步执行一个代码语句,更新变量和字段来“执行”程序。

    After some practice, you will be able to do this all in your head ... 经过一些练习,您将可以在脑海中完成所有任务...


I've only scratched the surface of this topic, but if you Google for "how to debug java", there are lots of good resources: tutorials, videos, and so on. 我只是从头开始讨论此主题,但是如果您在Google上搜索“如何调试Java”,那么这里有很多很好的资源:教程,视频等。


Finally, a couple of tips: 最后,一些技巧:

  • Be methodical. 有条理。 Understand the what the code actually says. 了解代码的实际含义。 Look at the evidence that you have. 查看您拥有的证据。

  • Don't rely on "hunches". 不要依靠“预感”。 It is easy to waste time looking for phantoms if you make assumptions about what is happening that are not evidence-based. 如果您对发生的事情做出的假设不是基于证据的,则很容易浪费时间寻找幻像。

  • Assume that 99% of the time, the bug will be in your code. 假设有99%的时间,该错误将在您的代码中。 And make that 99.99% if you are using just the standard Java class libraries. 如果仅使用标准Java类库,则占99.99%。

  • Debugging multi-threaded programs is hard ..... 调试多线程程序很难.....

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

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