0

我想我已经走得很远了,但我在逻辑上挂断了也许你们中的一些更聪明的家伙可以帮助我!

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

4

2 回答 2

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;
    }
}
于 2013-02-22T20:57:00.233 回答
0

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

在您的列表类中,您需要以下内容(以 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;
}

}

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

于 2013-02-22T18:32:36.860 回答