【问题标题】:Trouble understanding method for inserting nodes javajava插入节点的麻烦理解方法
【发布时间】:2013-02-22 18:26:25
【问题描述】:

我想我已经走得很远了,但我处于逻辑困境中,也许你们中的一些更聪明的家伙可以帮助我!

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 String排序,这是一个十六进制数,我不确定它是否明显但我想这样做从最小到最大的顺序。现在你可以看到我的代码变得非常复杂,很难遵循和处理,但我想我已经很接近了,任何人都可以提供任何提示或“逻辑指导”来帮助我更好地理解如何获得这个正常工作?在当前状态下,它正在破坏我的列表,有点循环,然后抛出 NullPointerException!

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

我现在非常接近,当我按照它们在列表中的顺序放入对象时它可以工作,但是如果我尝试插入一个属于中间某处的对象节点,我会破坏我的列表,我没有看到我的错误,有人看到了吗?主要参考

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 以使下一个对象比最后一个对象大,它会起作用,但如果它们按照现在的方式放置,则不会。

【问题讨论】:

    标签: java list methods insert linked-list


    【解决方案1】:

    尽量保持简单。不要落入陷阱,把所有东西都放在不同的“特殊”情况下。

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

    【讨论】:

    • 是的,我正试图弄清楚如何减少它,但仍然没有遗漏任何东西,哇,谢谢你的帮助
    【解决方案2】:

    首先,我一直被教导链表的以下结构

    在您的列表类中,您需要以下内容(以 sudo 代码显示)

    list class
    {
       Node head;
       Node current;
    
    
      constructor(Object O){
        Node n = new Node(O);
        head = n;
        current = head;
     }
    
     void addItem ( Object o)
    {
        Node n = new Node(o);
        if(head.nextNode() == null)
            head.nextNode(n);
        else
            current.nextNode(n);
        current = n;
    }
    
    }
    

    从我的示例中可以看出,您的列表类中应该有两个节点指针。一个指向列表的头部,一个指向当前节点。添加项目时,首先设置 current.Next,然后将 current 发送到下一个。这就是我认为你的问题所在。

    【讨论】:

    • 好吧,我在部分方法中使用了那些临时的“nodePtr”对象来处理这类问题,所以目前它不完全是个问题吗?只是真的很难遍历我的节点并使用所有这些检查和 while 循环来查看什么去哪里,然后把它贴在它所属的地方。把我的大脑变成椒盐脆饼!
    • 你是说你想做的是将它们放在链表上的地方吗?
    • 是的,我正在尝试通过链表并使用 ifs 处理所有这些情况,我需要通过节点持有的对象的 rfidTag 来组织节点。因此,例如,我有 5 个对象,它们的 rfidTag 分别为 1-5,我试图让它们按链表中的那个数字组织,并且由于所有的处理和案例处理,我试图用 ifs 和其他人并试图遍历以查看它在列表中的位置,我变得非常困惑和迷失。
    • 我现在正在编写代码,注意到一些问题,我会尽快更新,希望也能更好地描述我的意图。
    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2019-05-08
    • 2017-03-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多