简体   繁体   English

冒泡排序双链表Java

[英]Bubble Sort Doubly Linked List Java

I am trying to create a bubble sort on a doubly linked linked list in Java but am getting Null Pointer Exception errors. 我正在尝试在Java中的双向链接列表上创建冒泡排序,但我得到Null Pointer Exception错误。 I believe it to have an issue with when I call the getPrevious method on the head which of course has a value of null. 我认为当我在头上调用getPrevious方法时遇到问题,当然它的值为null。 However, I cannot think how to do the bubble sort without accessing the getPrevious method for the other nodes. 但是,我无法想到如何在不访问其他节点的getPrevious方法的情况下进行冒泡排序。

I can implement an if statement to check if its the head or tail of the list first, but I feel like there is a smarter way to do this. 我可以实现一个if语句来检查它是否是列表的头部或尾部,但我觉得有一种更聪明的方法可以做到这一点。

I also have been unable to run a successful build of this, so am not even sure the code will work. 我也无法成功运行这个,所以我甚至不确定代码是否可行。 If you have a different idea of how to implement this please let me know. 如果您对如何实现这一点有不同的想法,请告诉我。

Any suggestions are welcome! 欢迎任何建议!

 public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort
  {
      int i = 0;
      int j = 0;
      Node currentNode = list.head;
      Node previousNode = currentNode;
      Node tempNext =  currentNode;
      Node tempPrevious = currentNode;


      for(i=0; i<list.getSize(); i++)
      {
          for(j=0; j<list.getSize()-1; i++)
          {
              if(currentNode.getData() > currentNode.getNext().getData())
              {
                  tempNext = currentNode.getNext().getNext();
                  tempPrevious = currentNode.getPrevious();
                  currentNode.getPrevious().setNext(currentNode.getNext());
                  currentNode.getNext().setNext(currentNode);
                  currentNode.setPrevious(currentNode.getNext());
                  currentNode.setNext(tempNext);

              }

              currentNode = currentNode.getNext();

          }
      }



  }

So you have a double linked list. 所以你有一个双链表。 I assume each element contains some information... say an integer. 我假设每个元素都包含一些信息......比如一个整数。 It must also contain two pointers: one to the previous element and one to the next element. 它还必须包含两个指针:一个指向前一个元素,一个指向下一个元素。

Assuming this is true, notice that you don't have to modify the pointers because they already point from one element to another. 假设这是真的,请注意您不必修改指针,因为它们已经从一个元素指向另一个元素。 all you have to do is sort the values of the list elements so that the first item in the list has the lowest value, the second has the second lowest value and so on. 您所要做的就是对列表元素的值进行排序,使列表中的第一项具有最低值,第二项具有第二低值,依此类推。

You can do it like this: 你可以这样做:

public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort {
      int i = 0;
      Node currentNode = list.head;
      Node auxNode;
      int foundChange = 1;
      while(foundChange) {
        foundChange = 0;
        for(i=0; i<list.getSize()-1; i++) {
          if (currentNode.getData() > currentNode.getNext().getData()) {
            auxNode.setData(currentNode.getData());
            currentNode.setData(currentNode.getNext.getData());
            currentNode.getNext.setData(auxNode.getData());
            foundChange = 1;
          }
          currentNode = currentNode.getNext();
        }

}

If you haven't defined the setData method yet, then do so. 如果尚未定义setData方法,则执行此操作。 It must be similar to getData, but it will set the data of an object to the value it gets as a parameter instead of returning the value of the data in that object. 它必须类似于getData,但它会将对象的数据设置为它作为参数获取的值,而不是返回该对象中数据的值。

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

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