简体   繁体   English

为什么我的方法无法按字母顺序对链接列表进行排序?

[英]Why does my method fail to sort my linked list alphabetically?

public class doubleLinkedList {

    class Node {
      String value;
      Node prev;
      Node next;

      Node(String val, Node p, Node n) {
        value = val;
        prev = p;
        next = n;
      }

      Node(String val) {
        value = val;
        prev = null;
        next = null;
      }
    }

    Node first;
    Node last;

    public doubleLinkedList() {
      first = null;
      last = null;
    }

    public boolean isEmpty() {
      if (first == null)
        return true;
      else
        return false;
    }

    /**The size method returns the length of the linked list
     * @return the number of element in the linked list
    */
    public int size() {
      int count = 0;
      Node traverse = first;
      while (traverse != null) {
        count++;
        traverse = traverse.next;
      }
      return count;
    }


    public void add(String element) {

      if (isEmpty()) {
        first = new Node(element);
        last = first;
      } else {

        Node p = first;
        Node elementTobeAdded;
        while (((p.value).compareTo(element)) > 0 && p.next != null) {
          p = p.next;
        }

        if (p.next != null) {
          elementTobeAdded = new Node(element, p, p.next);
          p.next.prev = elementTobeAdded;
          p = elementTobeAdded.prev;
        } else {
          elementTobeAdded = new Node(element, p, null);
          p.next = elementTobeAdded;
          elementTobeAdded.next = null;
          last = elementTobeAdded;
        }

      }
    }

    public void printForward() {
      Node printNode = first;
      while (printNode != null) {
        System.out.print(printNode.value + ", ");
        printNode = printNode.next;
      }
    }
  }
  public class test {

    public static void main(String[] args) {
      doubleLinkedList car = new doubleLinkedList();
      car.add("Jeep");
      car.add("benz");
      car.add("Honda");
      car.add("Lexus");
      car.add("BMW");
      car.printForward();
    }
  }

My add method is trying to add nodes to a list in alphabetical order. 我的add方法尝试按字母顺序将节点添加到列表中。 My printForward method prints out each element in the list. 我的printForward方法打印出列表中的每个元素。 In my main method, it prints out "Jeep, benz, Honda, BMW,", which is not in alphabetical order. 在我的主要方法中,它打印出“吉普,奔驰,本田,宝马”,但不是按字母顺序排列。

Change the not empty case for your add method from this 从这里更改add方法的非空大小写

  Node p = first;

  Node elementTobeAdded;

  while(((p.value).compareTo(element)) > 0 && p.next != null)
  {
    p = p.next;
  }

  if(p.next != null)
  {
  elementTobeAdded = new Node(element,p,p.next);
  p.next.prev = elementTobeAdded;
  p = elementTobeAdded.prev;
  }

  else
  {
    elementTobeAdded = new Node(element, p, null);
    p.next = elementTobeAdded;
    elementTobeAdded.next = null;
    last = elementTobeAdded;
  }

to this: 对此:

  Node p = first;
  while (p.value.compareTo(element) < 0 && p.next != null) {
    p = p.next;
  }
  if (p.value.compareTo(element) > 0) {
    Node toAdd = new Node(element, p.prev, p);
    p.prev = toAdd;
    if (toAdd.prev != null) {
        toAdd.prev.next = toAdd;
    }else {
      first = toAdd;
    }
  }else {
    Node toAdd = new Node(element, p, p.next);
    p.next = toAdd;
    if (toAdd.next != null) {
        toAdd.next.prev = toAdd;
    }else {
      last = toAdd;
    }
  }

There were many errors here. 这里有很多错误。 The biggest one was that you never checked for the case where the new element should be inserted at the beginning of the list. 最大的问题是您从未检查过将新元素插入列表开头的情况。 A new element was always inserted after the first element even if it should have come first. 即使第一个元素应该放在最前面,也总是在其后插入一个新元素。

Note that "benz" comes at the end because the String.compareTo method treats capitals as coming before lower case letters. 请注意,“ benz”位于末尾,因为String.compareTo方法将大写字母视为小写字母之前的大写字母。

It is not an a linked list... You wrote some sort of Queue (with optional possibility to make it Dequeue). 它不是一个链表...您编写了某种队列(可以选择使它出队)。

About your question - you have an error in your 'add' method - at least you don't check if it is necessary to move head forward. 关于您的问题-您的“添加”方法存在错误-至少您不检查是否有必要前进。 It is possible that you have another bugs, but it is too hard to read such styled sources (please fix your question formatting)... 您可能还有其他错误,但是很难读取此类样式的源代码(请修复您的问题格式)...

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

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