简体   繁体   中英

Sorted List insertion keeps creating new head?

Everytime I want to insert a new node into the list it will make a new head, which it should do the first time, but the second time it should attach the new node to the previous one. Whenever I insert with this code it keeps creating a new head. Why does the previous head which was first inserted not being saved?

static class TreeNode{
    int frequency;
    boolean isLeftChild;
    TreeNode parent;
    TreeNode next;

    /**
     * TreeNode class constructor to initialize the variables and also
     * takes a frequency as the parameter.
     * @param f Frequency of a certain character.
     */
    TreeNode(int f){
        frequency = f;
        isLeftChild = true;
        parent = null;
        next = null;
    }
}

// Class used to store information for the linked list.
static class List{
    TreeNode head;
    int numItems; // number of nodes in the list

    List(){
        head = null;
        numItems = 0;
        // initialize head and numItems
    }

    /**
     * Inserts a node into the TreeNode linked list according to its frequencies
     * position as it will be in a SORTED list.
     * @param freq Frequency of a specific character.
     * @return Returns the new TreeNode object that has been inserted.
     */
    TreeNode insert(int freq){
        TreeNode previous, current, newNode;
        int newFreq = freq;
        numItems++;

        previous = null;
        current = head;
        while((current != null) && (Integer.valueOf(newFreq).compareTo(Integer.valueOf(current.frequency)) > 0 )){
            previous = current;
            current = current.next;
        }
        if(previous == null){
            head = new TreeNode(newFreq);
            return head;

        }
        else{
            newNode = new TreeNode(newFreq);
            previous.next = newNode;
            return newNode;
        }

    }

If the head should only be created once, then that's what the constructor is for!

List() {
    // initialize head here
    head = new Node();
    head.next = head;
    head.previous = head;
}

Also, I think you need to think a bit longer about what you want your list's nodes to look like. Do I understand correctly that you're trying to create a doubly linked sorted list? If so, it's probably of most use if its nodes contain data objects of type Comparabe , so that you can reuse the list for other purposes. (Even fancier would be to make its data type generic Comparable<T> .)

Finally, if you're trying to store frequencies of letters in the list, then the data type will need to have two fields, one for the letter's frequency and one for the letter it's counting. And you'll have to decide which of the two you wish to use for sorting: the character or the frequency. I'd expect the character.

Anyways, try to draw a little picture first of what you'd like to do, before you start coding away.

Let me take a stab at this:

Say that you first insert TreeNode s of frequencies 5 and 4, in that order. As you said, the first insertion will occur as intended. When you attempt to insert the TreeNode with frequency 4, the while(...) will be bypassed immediately, meaning that current is still the TreeNode with frequency 5, and previous is still null . According to your code, this will cause the Linked List to create a new head, and overwrite the previous one.

In short, you are not assigning correctly if the TreeNode with frequency you inserted is less than the frequency of the head. To correct this, something like this should be added after your while loop:

if(current == head) {
  newNode = new TreeNode(newFreq);
  newNode.next = head;
  head = newNode;
}

Also, in the "else" part if your if/else statement, you are not assigning the "next" field of newNode , so this statement should be added:

newNode.next = current;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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