gpt4 book ai didi

java - 填充并打印个人 LinkedList 复制品的元素

转载 作者:行者123 更新时间:2023-12-02 09:07:09 25 4
gpt4 key购买 nike

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

此代码应该首先打印“此列表为空”。然后在一行中打印 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());
}
}

代码打印“此列表为空。”一次然后似乎永远不会到达第二个打印输出行,但没有异常(exception),通知,任何东西。我怀疑问题隐藏在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
}

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

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

再想一想,让我立即添加 .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();
}
}
}

ListItem 类:

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);
}

节点类:

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;
}
}
}

最佳答案

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

    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

关于java - 填充并打印个人 LinkedList 复制品的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com