简体   繁体   中英

Double Linked List with String Java

I wrote the following program:

public class DLinkedList<T extends Comparable<T>> {

    private class DNode<T> {
        private DNode<T> prev, next;
        private T nodeValue;
        public DNode() {
            nodeValue = null;
            next = prev = this;
            }

        public DNode(T val) {
            nodeValue = val;
            next = prev = this;
            }
        }

    private DNode<T> header;

    private int size;

    public DLinkedList() {
        header = new DNode<T>();
        size = 0;
    }

    public Boolean empty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public void addOrder(T val) {
        DNode<T> newNode = new DNode<T>(val);
        DNode<T> curr = header.next;
        int count = 0;

        while (curr != header &&
            curr.nodeValue.compareTo(newNode.nodeValue) < 0 ) {

            curr = curr.next;
            newNode.prev = curr;
            newNode.next = curr.next;
            newNode.next.prev = curr;
            count++;
        }

        if (count == 1) {
            newNode.prev = curr;
            newNode.next = curr.prev;
            curr.prev.next.next = newNode;

        } else {
            newNode.prev = curr.prev;
            newNode.next = curr;
            curr.prev.next = newNode;
            count++;
        }
    }

    public String toString() {
        String str = "";
        DNode<T> curr = header.next;
        while (curr != header) {
            str += curr.nodeValue + ", ";
            curr = curr.next;
        }
        if (str.length() > 0) {
            return str.substring(0, str.lastIndexOf(", "));
        } else {
            return str;
        }
    }

    public static void main(String[] args) {
            DLinkedList<String> d = new DLinkedList<String>();
            d.addOrder("t");
            d.addOrder("y");
            d.addOrder("e");
            d.addOrder("b");
            d.addOrder("p");
            System.out.println("List 1: t, y, e, b, p" + "\nList 1 added and sort : " +d);
    }
}

I am confused/lost as to how to fix my issue. I want to create a double linked list of nodes that hold String values and as I add those string values into the list, the list auto-sort it by way of insertion sort.

I start it off with a node call header which has a null value. Then as I add the String "t, y, e, b, p" into the list using the addOrder(T val) method everything seem to work. It will print out in sorted order "b, e, p, t, y".

The problem occur if I decided not to print the "p" at the end and instead do "t, y, e, b, c" all I get print out is "b, c" instead of doing "b, c, e, t, y". If I add an "a" instead of a "p" or "c" at the end of the list, everything seem to be fine printing "a, b, e, t, y". So it seem like it work with everything that is not "c, d, e". Seem like I need to code a method to add a newNode that will go between them which I'm at lost as how to do it now.

Another problem occurs if I decided to add string "t, y, e, v, p". It seems like this just prints "e, p". Which seems like it added "t, y, e, v" but when "p" comes in, it dropped "t, y, v" and left "e, p".

It seems like I can't add anything between "ft", but anything before or after that is fine. I have a feeling it has something to do with my index "curr" that maybe it's stuck with my node header.

Some of your code seems a bit confused. You have what looks like a loop for skipping through the list until you find the insertion point, but it changes both the new node's pointers and the existing. I would just have curr = curr.next in that loop, and make no changes at all in the new node's pointers or the existing list until after you have found where to insert, and then remember you need to change a total of four pointers: the next and previous in the new node, the next pointer in its predecessor, and the previous pointer in its successor.

            while (curr != header &&
                    curr.nodeValue.compareTo(newNode.nodeValue) < 0 ) {

                    curr = curr.next;
                    newNode.prev = curr;
                    newNode.next = curr.next;
                    newNode.next.prev = curr;
                    count++;
            }

I have a web page on how to debug that you may find helpful.

I agree with the code being a bit confusing and that you should remove the assignments from the while loop but the fix seems simple.

in the section after the loop you need to switch your assignments w/o the count check:

newNode.prev = curr.prev;
newNode.next = curr;
curr.prev.next = newNode;
curr.prev = newNode;

in the example of using "t, y, e, v, p" when inserting "p" the current cursor is on the letter "t". At this point, you want "p" to point to "t" and the previous to be "t" previous, in this case "e".

I haven't tested your other scenarios yet but this looks like what you were missing.

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