填充和打印个人 LinkedList 复制的元素

Populating and printing the elements of a personal LinkedList reproduction

这将有点难以说明,因为代码在几个 class 之间分开,但我自己无法弄清楚。

此代码应该首先打印 "This list is empty.",然后在一行中打印 stringData 中的每个单词:

public class Main
{
    public static void main(String[] args)
    {
        LinkedList_1 list1 = new LinkedList_1(null);
        list1.printList(list1.getRoot());

        String stringData = "Harrier Ken Klaasje Titus Evrart Manaaba Cindy Eyckhead";
        String[] data = stringData.split(" ");
        for (int i = 0; i < (data.length); i++) {
            list1.addItem(new Node(data[i]));
        }
        list1.printList(list1.getRoot());
    }
}

代码打印 "This list is empty." 一次,然后似乎永远不会到达第二个打印输出行,但没有异常、通知等任何内容。我怀疑问题隐藏在list1.addItem,它看起来像这样(评论只针对我自己):

        @Override
    public boolean addItem(ListItem newItem)
    {
        if (this.root == null) { // If no other item on list, newItem = first item on list
            this.root = newItem;
            return true;
        }
        ListItem current = this.root; // current = first item on list
        while (current != null) { // As long as current exists, do
            int comparison = (current.CompareTo(newItem)); // Alphabetically compares newItem and current. >0 = newItem comes AFTER current. <0 = newItem comes BEFORE current. 0 = newItem and current are identical.
            // ........................................................................................................
            if (comparison < 0) { // newItem should be inserted AFTER current
                if (current.getNext() != null) { // If the list contains another item after current, then
                    current = current.getNext(); // current becomes next item in list.
                    // outcome AFTER-NOT LAST YET is completed and the while loop will repeat until newItem is either last or no longer comes after
                } else { // if current is only item on list, then
                    current.setNextItem(newItem); // newItem comes after current
                    newItem.setPreviousItem(current); // current comes before after
                    return true; // outcome AFTER-LAST completed ---> drop out of the method/loop
                } // ........................................................................................................
            } else if (comparison > 0) { // newItem comes BEFORE current
                if (current.getPrevious() != null) { // if current is not first in list, then
                    current.getPrevious().setNextItem(newItem); // newItem comes after item that comes before new item (1. previous 2. newItem)
                    newItem.setPreviousItem(current.getPrevious()); // previous comes before newItem
                    newItem.setNextItem(current); // current comes after newItem (1.previous 2. newItem 3. current)
                    current.setPreviousItem(newItem); // newItem comes before current
                    // outcome BEFORE-NOT FIRST YET is completed and the while loop will repeat until newItem is either first or no longer comes before
                } else { // if current is first in list
                    newItem.setNextItem(this.root); // newItem comes before root (=current)
                    this.root.setPreviousItem(newItem); // root (=current) comes after newItem
                    this.root = newItem; // newItem is now root
                    return true;  // outcome BEFORE-FIRST completed ---> drop out of the method/loop
                }// ........................................................................................................
            } else { // curent == newItem. As a duplicate, newItem is not added to the list
                System.out.println(newItem.getValue() + " is already part of the List.class Duplicate not added.");
                return false; // drop out of the method/loop without adding newItem to the list
        }   }
        return false; // drop out of the loop without adding newItem to the list (how you would ever get here I don't know
    }

现在也许有人已经发现了问题,或者它可能位于其他 classes 之一。由于我不想将整个代码(尽管这是一个相当短的培训程序)粘贴到一个新线程中,也许您可​​以告诉我缺少哪些信息以了解问题,然后我添加或删除特定的部分?

无论如何,在此先感谢您的帮助。

再三考虑,让我立即添加 .printList 的功能。

    public void printList(ListItem root)
    {
        if (root == null) {
            System.out.println("This list is empty.");
        } else {
            while (root != null) {
                System.out.println(root.getValue());
                root = root.getNext();
            }
        }
    }

列表项class:

public abstract class ListItem
{
    protected ListItem previousItem = null;
    protected ListItem nextItem = null;
    protected Object value;

    public ListItem(Object value) {
        this.value = value;
    }
    public Object getValue() {
        return value;
    }
    public void setValue(Object value) {
        this.value = value;
    }

    abstract ListItem getNext();
    abstract ListItem getPrevious();
    abstract void setNextItem(ListItem n);
    abstract void setPreviousItem(ListItem p);
    abstract int CompareTo(ListItem item);
}

节点class:

public class Node extends ListItem
{

    public Node(Object value) {
        super(value);
    }

    @Override
    ListItem getNext() {
        return this.nextItem;
    }
    @Override
    ListItem getPrevious() {
        return this.previousItem;
    }
    @Override
    void setNextItem(ListItem n) {
        this.nextItem = n;
    }
    @Override
    void setPreviousItem(ListItem p) {
        this.previousItem = p;
    }
    @Override
    public Object getValue() {
        return value;
    }

    @Override
    int CompareTo(ListItem item)
    {
        if (item != null) {
            return ((String)super.getValue()).compareTo((String)item.getValue());
        } else {
            return -1;
        }
    }
}

在一种情况下您忘记了 return 这就是它进入无限循环的原因。

    if (comparison > 0) { // newItem comes BEFORE current
                if (current.getPrevious() != null) { 
                    current.getPrevious().setNextItem(newItem); 
                    newItem.setPreviousItem(current.getPrevious()); 
                    newItem.setNextItem(current); 
                    current.setPreviousItem(newItem); 
                    // return when element is added successfully