插入已排序的链接列表

时间:2014-12-07 21:17:25

标签: java

我尝试使用插入,删除和检查是否存在整数方法来实现已排序的链接列表,而且我目前无法理解为什么我的插入方法不能正常工作。它将整数插入到链表中,但是它们是向后排序的,我已经尝试过调整条件,但它没有按照应有的方式工作。

void insert(int x){
    LinkedList newLink = new LinkedList(x);
    if (front == null) {
        front = newLink;
    } else if (x > front.x){
        newLink.next = front;
        front = newLink;
    } else {
        LinkedList current = front.next;
        LinkedList before = front;
        while (current != null){
            if (x < front.x) {
                before = current;
                current = current.next; 
            }
            newLink.next = before.next;
            before.next = newLink;
        }
    } 
}

4 个答案:

答案 0 :(得分:0)

else if (x > front.x){
    newLink.next = front;
    front = newLink;
}

如果新链接更大而不是前端,则将新节点设为front节点,并将其下一个节点设置为最后一个节点。您将按反向排序顺序插入列表的前面。

if (x < front.x) {
    before = current;
    current = current.next; 
}

您始终与front节点进行比较,而不是current节点。

答案 1 :(得分:0)

我认为它会解决您的问题,而且,我添加了一些注释,以便您了解我的代码更改:

void insert(int x) {
    LinkedList newLink = new LinkedList(x);
    if (front == null) {
        front = newLink;
        // insert in head if x is lower than the head
    } else if (x <= front.x) {
        newLink.next = front;
        front = newLink;
    } else {
        // find the first node which value is lower than x (or the tail)
        LinkedList current;
        for (current = front; current.next != null && x >= current.next.x ; current = current.next);
        // to remove duplicates
        if (x != current.x) {
            newLink.next = current.next;
            current.next = newLink;
        }
    }
}

答案 2 :(得分:0)

请查找附件我的实施。请根据您的需要使用和修改。

注意!此实现将丢弃相等的值!

/******************************************************************
 * Adds the given string to the sorted list. If the string already
 * is in the list, then the list will not be modified.
 * 
 * @param list The sorted list of strings
 * @param str The string to be inserted if not there already
 * @return Returns true if the list has changed otherwise false
 */
public static boolean
add2SortedList( List<String> list, String str ) {
    int res = 0;

    //--- Loop through list from end to front
    for ( int i = list.size() - 1; i >= 0 ; i-- ) {

        //--- Compare to current entry
        res = str.compareTo( list.get( i ));

        //--- Already in list => return
        if ( res == 0 )
            return false;

        //--- Bigger than current one => insert after
        if ( res > 0 ) {
            list.add( i + 1, str );
            return true;
        }
    }

    //--- Insert at head of list
    list.add( 0, str );
    return true;
}


/******************************************************************
 * Adds the given value to the sorted list if its not already there.
 * 
 * @param list the sorted list of values
 * @param value the value to be inserted if not there already
 * @return returns true when value was added, else false
 */
public static boolean
add2SortedSet( List<Integer> list, Integer value )
{
    int size = list.size();
    for ( int i = 0 ; i < size ; i++ )
    {
        int res = value.compareTo ( list.get( i ));

        //----- Insert before this element
        if ( res < 0 ) {
            list.add( i, value );
            return true ;
        }

        //----- New element already in list
        if ( res == 0 )
            return false ;
    }

    //----- Append to end of list
    list.add ( value );
    return true ;
}

答案 3 :(得分:-1)

在linkedList中插入的默认方式:

LinkedList<Integer> myList = new LinkedList<>();

myList.add(238);
myList.add(7);
..        
Collections.sort(myList);

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

以不同的方式排序使用接口Comparable