Inserting a number into a linked list and keeping it sorted while doing so in java

邮差的信 提交于 2019-12-25 18:04:33

问题


I feel very confident that i'm right in this code. Logically, it makes sense to me but for some reason the program refuses to run past a certain point. I'm supposed to do this without using proprietary classes or hashtables. My list node is a basic singly linked list. Assuming i have a dummy list at first, 0, i'm able to add one number to the list but that is all. This is the method that will not work beyond adding the first number.

Assuming my list is 0 -> 2. and i'm trying to add 1.

public void insert(int newElement) {
    List marker = head;
    List temp = new List(newElement, null);

    if (head.next == null) {
        head.next = temp;
    } else {
        while (marker.next != null) {
            if (newElement < marker.next.value) {
                temp.next = marker.next;
                marker.next = temp;
                marker = marker.next;
            }
        }
    }
}

回答1:


    List marker = head;
    List temp = new List(newElement, null);

    if (head.value > temp.value) {
        head.next = new List(head.value, null);
        head.value = newElement;
    }else if(head.next == null)
    {
        head.next = temp;
    }
    else {
        while (marker.next != null) {
            if (newElement < marker.next.value) {
                temp.next = marker.next;
                marker.next = temp;
                break; //we added the node, no need to continue looping
            }
            else //we need to iterate to the next node in the list until empty
            {
                marker = marker.next;
            }
        }
    }



回答2:


This seems to be working and I'm currently testing it with good results. Can anyone find a problem with this code that i'm missing?

public void insert(int newElement) {
    List marker = head;
    List temp = new List(newElement, null);

    if (head.next == null) {
        head.next = temp;
    } else {
        for (marker = head; marker.next != null; marker = marker.next) {
            if (temp.value < marker.next.value) {
                temp.next = marker.next;
                marker.next = temp;
                break;
            }

        }
        if (marker.next == null && temp.value > marker.value) {
                marker.next = temp;

            } 
    }

}



回答3:


public void insert(int val) {
    Item item = new Item(val);

    // the case when there is no item (not counting the dummy head)
    if (head.getNext() == null) {
        head.setNext(item);
        item.setNext(null);
    } else {
        // if there is at least one item....
        Item cursor = head.getNext();
        Item prev = cursor;

        // simply keep looping the list until the new value is less than a value in list
        // if the new value is greater than all the values in the list...
        // then the do-while loop will break when null is reached...
        // at the end of the list
        do {
            if (val < cursor.getVal()) {
                // break and insert
                break;
            }
            prev = cursor;
            cursor = cursor.getNext();
        } while (cursor != null);

        // insert the item
        item.setNext(cursor);

        // the case when the new value is the smallest and is to be inserted at head
        if (val < head.getNext().getVal()) {
            head = item;
        } else prev.setNext(item);
    }
}

This is your code snippet:

if (newElement < marker.next.value) {
    temp.next = marker.next;
    marker.next = temp;
    marker = marker.next;
}

Take a pencil and a paper and trace out this piece of code. You will see what's wrong with it. Take a look at this answer and see the image attached. This is how you should really debug problems regarding linked lists. That image is not specific to your code, but it should give you an idea how to approach this kind of problems.



来源:https://stackoverflow.com/questions/40939732/inserting-a-number-into-a-linked-list-and-keeping-it-sorted-while-doing-so-in-ja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!