java - 麻烦理解java插入节点的方法

标签 java list methods insert linked-list

我想我已经走了很远了,但我遇到了逻辑上的困境,也许你们中的一些聪明人可以帮助我!

public class ItemList{

ItemInfoNode head;
ItemInfoNode tail;

int listCount = 0;

public ItemList(){

    head = tail = null;

}

public void insertInfo(String name, String rfidTag, String initPosition, double price){

    ItemInfo obj = new ItemInfo(name, rfidTag, initPosition, initPosition, price);
    ItemInfoNode temp = new ItemInfoNode();
    temp.setInfo(obj);

    if(head == null){ head = tail  = temp; }

    else{

        if(head == tail){//BEGIND SECOND OBJECT HANDLING

            if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0){//to see if temp belongs after head

                head.setNext(temp);
                temp.setPrev(head);
                tail = temp;

            }

            else{

                ItemInfoNode nodePtr = head;

                head = temp;
                tail = nodePtr;
                head.setNext(tail);
                tail.setPrev(head);

            }
        }//END SECOND OBJECT HANDLING

        else{

            if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){

                ItemInfoNode nodePtr = head;

                head = temp;
                temp.setNext(nodePtr);
                temp.getNext().setPrev(head);

            }

            else if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0 && tail.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){

                head.setNext(temp);
                temp.setPrev(head);

            }

            else{//item bigger then tail

                ItemInfoNode nodePtr = tail;

                tail = temp;
                tail.setPrev(nodePtr);
                tail.getPrev().setNext(tail);

            }
        }
    }

    listCount++;

}}

现在这个方法的目的显然是在它们所属的位置插入节点,但是它们需要按它们的 rfidTag 字符串排序,这是一个十六进制数字,我不确定它是否明显,但我想这样做从最小到最大的顺序。现在,正如您所看到的,我的代码已经变得非常复杂,非常难以遵循和处理,但我认为我已经很接近了,任何人都可以提供任何提示或“逻辑指导”,以帮助我更好地理解如何实现这一点能正常工作吗?在当前状态下,它正在破坏我的列表,有点循环,然后抛出 NullPointerException!

编辑**:所以我修改了我的代码并添加了注释,以更简洁地解释我想要完成的任务,也许有人可以帮助我理解现在如何处理这些方法?

我现在非常接近了,当我按照对象在列表中的顺序放入对象时,它会起作用,但是如果我尝试插入属于中间某个位置的对象节点,我会破坏我的列表,我没有看到我的错误,有人看到吗?主要供引用

public class Test{

public static void main(String args[]){

    ItemInfo item = new ItemInfo(null, null, null, null, 0);

    item.setName("Chocolate");
    item.setTag("2");
    item.setOrigin("s12345");
    item.setCurrent("s12345");
    item.setPrice(30.00);

    ItemInfo item2 = new ItemInfo(null, null, null, null, 0);

    item2.setName("Buzz Lightyear");
    item2.setTag("1");
    item2.setOrigin("d67890");
    item2.setCurrent("d67890");
    item2.setPrice(15.99);

    ItemInfo item3 = new ItemInfo(null, null, null, null, 0);

    item3.setName("Hotwheels");
    item3.setTag("000000000");
    item3.setOrigin("h34743");
    item3.setCurrent("h34743");
    item3.setPrice(24.25);

    ItemInfo item4 = new ItemInfo(null, null, null, null, 0);

    item4.setName("Barbie");
    item4.setTag("FFFFFFFFF");
    item4.setOrigin("s49862");
    item4.setCurrent("s49862");
    item4.setPrice(21.22);

    ItemInfo item5 = new ItemInfo(null, null, null, null, 0);

    item5.setName("Bicycle");
    item5.setTag("CCCCCCCCC");
    item5.setOrigin("k28475");
    item5.setCurrent("k28475");
    item5.setPrice(10.99);

    ItemInfoNode nood = new ItemInfoNode();
    ItemInfoNode nood2 = new ItemInfoNode();
    ItemInfoNode nood3 = new ItemInfoNode();
    ItemInfoNode nood4 = new ItemInfoNode();
    ItemInfoNode nood5 = new ItemInfoNode();

    nood.setInfo(item);
    nood2.setInfo(item2);
    nood3.setInfo(item3);
    nood4.setInfo(item4);
    nood5.setInfo(item5);

    ItemList list = new ItemList();

    list.insertInfo(item.getName(), item.getTag(), item.getCurrent(), item.getPrice());
    list.insertInfo(item2.getName(), item2.getTag(), item2.getCurrent(), item2.getPrice());
    list.insertInfo(item3.getName(), item3.getTag(), item3.getCurrent(), item3.getPrice());
    list.insertInfo(item4.getName(), item4.getTag(), item4.getCurrent(), item4.getPrice());
    list.insertInfo(item5.getName(), item5.getTag(), item5.getCurrent(), item5.getPrice());

    list.printAll();

}

}

还有我的输出...

风火轮 自行车

现在,如果我更改 5 个对象的 rfidTags,使下一个比上一个更大,它会起作用,但如果它们按照现在的方式放置,则不起作用。

最佳答案

尽量保持简单。不要陷入将所有情况划分为不同“特殊”情况的陷阱。

public void insertInfo(String name, String rfidTag, String initPosition, double price){

    ItemInfo obj = new ItemInfo(name, rfidTag, initPosition, initPosition, price);
    ItemInfoNode addition = new ItemInfoNode();
    addition.setInfo(obj);
    ++listCount;

    // Walk to the item following:
    ItemInfoNode insertionNext = head;
    while (insertionNext != null
            && insertionNext.getInfo().getTag().compareTo(rfidTag) >= 0) {
        insertionNext = insertionNext.next;
    }

    ItemInfoNode insertionPrevious = insertionNext == null ? tail
        : insertionNext.previous;

    // Prepare addition itself:
    addition.next = insertionNext;
    addition.previous = insertionPrevious;

    // The next link backwards should point to the addition:
    if (insertionNext == null) {
        tail = addition;
    } else {
        insertionNext.previous = addition;
    }

    // The previous link forwards should point to the addition:
    if (insertPrevious == null) {
        head = addition;
    } else {
        insertPrevious.next = addition;
    }
}

关于java - 麻烦理解java插入节点的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030767/

相关文章:

java - 默认 GWT 复选框为未选中状态

python - 过滤包含元组的列表的列表

c# - 如何检查列表是否已排序?

Java:初学者帮助获取变量

ios - 如何知道 View 何时会从后台变为事件状态

java - Log4j2 + Java:以编程方式添加asyncLogger

尝试录制声音时出现 javax.sound.sampled.LineUnavailableException

python - 为什么我们不能 **unsplat 'self' 到一个方法中?

java - 如何使用spring动态加载java中的配置

java - 无法获取Java中特定索引中的对象