繁体   English   中英

如何将自定义链接列表的add方法转换为Java中的插入排序方法?

[英]How would I convert my custom linked list add method into an insert sort method in java?

我在Java中有此自定义链接列表(不是Java集合LinkedList)。 它将项目添加到列表中的选定位置。

public void add(T item, int position) {
    Node<T> addThis = new Node<T>(item);
    Node<T> prev = head;
    int i;

    if(position <= 0) {
        //throw new ListException("Cannot add element before position 1");
        System.out.println("Error: Cannot add element before position 1.");
    }

    else if(position == 1) {
        addThis.setNext(head);
        head = addThis;
    } else {
        for(i = 1; i < position-1; i++) {
            prev = prev.getNext();
            if(prev == null) {
                //throw new ListException("Cannot add beyond end of list");
                System.out.println("Cannot add beyond end of list");
            }
        } // end for
        addThis.setNext(prev.getNext());
        prev.setNext(addThis);
    }
} // end add

我要执行的操作不是将项目添加到选定位置,而是要按字母顺序添加项目,以便在插入时按字母顺序对整个列表进行排序。 这是我到目前为止的代码,但我遇到了麻烦。 我将如何让它做我想要的?

public void add(String title) {
    DvdNode addThis = new DvdNode(title);
    if (head == null) {
        head = addThis;
    } else if(title.compareToIgnoreCase(head.getTitle()) < 0) {
        // title comes before the current, so add as the first Dvd in the list
    } else {
        // the new title belongs somewhere later in the list
        // while i less than size of the list compare and insert if its greater
        // than what its being compared to
        // also update the links for the list so the whole list is still accessible
    } 
}

编辑:我终于得到它的工作。 我的解决方案基于Byakuya的解决方案。

public void add(Dvd item) {
  DvdNode addThis = new DvdNode(item);
  if(head == null) {
    head = addThis;
  } else if(item.getTitle().compareToIgnoreCase(head.getItem().getTitle()) < 0) {
      addThis.setNext(head);
      head = addThis;
    } else {
        DvdNode temp;
        DvdNode prev;
        temp = head.getNext();
        prev = head;
        while(prev.getNext() != null && item.getTitle().compareToIgnoreCase
            (prev.getNext().getItem().getTitle()) > 0) {
          prev = temp;
          temp = temp.getNext();
        }
        addThis.setNext(temp);
        prev.setNext(addThis);
      }
}

创建类似这样的内容:

public void add(String title){
    DvdNode addThis = new DvdNode(title);
    DvdNode iter = head;
    DvdNode prev = null;

    while (iter && addThis.name.compareTo(iter.name) < 0){
        prev = iter;
        iter = iter.getNext();
    }
    addThis.setNext(iter);
    addThis.setPrev(prev);
    if (prev)
        prev.setNext(addThis);
    if (iter)
        iter.setPrev(addThis);
    if (head == null)
        head = addThis;
}

如果head为null,则只需将addThis next和prev设置为null并更新head。 否则,迭代器将在第一个元素上在名称上大于或等于您的String标题的位置处停止(因此,如果您的String标题是列表中最大的,则迭代器将以null停止,然后将元素插入为列表中的最后一个元素,这是正确的),而prev将是迭代之前的最后一个元素。 然后进行正确的链接。

我的例子适合双向清单。 如果您希望它适用于单向列表,只需删除addThis和iter的Prev设置即可。

暂无
暂无

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

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