为什么我的方法不能按字母顺序对链表进行排序?


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();
    }
  }

我的add方法试图按字母顺序将节点添加到列表中。我的printForward方法打印出列表中的每个元素。在我的主要方法中,它打印出"Jeep, benz, Honda, BMW",这不是按字母顺序排列的。

将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;
  }

:

  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;
    }
  }
这里有很多错误。最大的错误是,您从未检查新元素应该在列表开头插入的情况。新元素总是在第一个元素之后插入,即使它应该在第一个元素的前面。

注意"benz"出现在末尾,因为String.compareTo方法将大写字母视为出现在小写字母之前。

这不是一个链表…你写了某种Queue(可以选择让它Dequeue)

关于你的问题——你的"add"方法有一个错误——至少你没有检查是否有必要向前移动头部。这是可能的,你有其他的错误,但它是太难阅读这样的样式源(请修复您的问题格式)…

相关内容

  • 没有找到相关文章

最新更新