0

I think I have gotten pretty far, but I am at a logical hang-up maybe some of you smarter fellows can help me out!

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

}}

Now the purpose of this method is obviously to insert nodes where they belong, but they need to be sorted by their rfidTag String, which is a hexadecimal number, I'm not sure if it is apparent but I want to do it from least to greatest order. Now as you can see my code has become very convoluted and very difficult to follow and deal with, but I think I am close, anyone have any tips or "logical guidance" they can give to help me better understand how to go about getting this to work properly? In its current state it is destroying my list, somewhat looping, and then throwing a NullPointerException!

EDIT**: So I have revised my code and added comments to show a more concise interpretation of what I am looking to accomplish, maybe someone can help me understand how to hand these methods now?

I am very close now, it works when I put in objects in the order in which they would be in the list, but if I try to insert an object node that belongs somewhere in the middle I destroy my list, I am not seeing my mistake, anyone see it? Main for reference

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

}

}

And my output as well...

Hotwheels Bicycle

Now if I change the rfidTags of the 5 objects so that the next one is larger then the last, it works, but not if they are put in the way they are now.

2 Answers 2

1

Try to keep it simple. Do not fall in the trap, of dividing all in different "special" cases.

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I was trying to figure out how to reduce it all but still not miss anything, wow thanks for the help
0

Well first off I have always been taught the following structure for linked lists

in your list class you need the following (presented in sudo code)

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

}

As you can see from my example you should have in your list class two Node pointers. One that points to the Head of the list and one that points to the current node. When you add an item, first you set the current.Next then you sent the current to the next. This is where I think your problem is.

4 Comments

Well I somewhat have used those temporary "nodePtr" objects in parts of the method to deal with that kind of issue, so it isn't like entirely a problem at the moment? Just really have a hard time traversing my nodes and using all these checks and while loops to see what goes where and then stick it where it belongs. Turning my brain into a pretzel!
Are you saying what you want to do is place things where they go on a linked list?
Yeah see, I am trying to get through the linked list and handle all these cases with the ifs, I need to get the nodes organized by the rfidTag of the object that the node holds. So for example, I have 5 objects, their rfidTag is 1-5 respectively, I am attempting to get them organized by that number in the linked list, and because of all the handling and case dealing I am trying to do with the ifs and elses and trying to traverse to see where it belongs in the list, I am getting extremely confused and lost.
I am working on the code right now, noticing a few issues, I will update shortly, hopefully with a better description of my intentions too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.