Question

I'm looking to use a method that takes in the information of an object, creates an instance of the object, sets the information, then it creates a node and sets the information to the node, and finally inserts the node into my linked list where it belongs. The linked list only has to be organized by the rfidTag String type which is a 9-digit hexadecimal representation. Here is what I have so far (I've ignored the "by rfidTag" part)...

public class ItemList {

    ItemInfoNode head;
    ItemInfoNode tail;
    ItemInfoNode cursor;

    int listCount = 0;

    public ItemList(){
        head = cursor = 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);
    }
}

Now I don't have the slightest clue as to what to put, but I'll show you what I've tried and add comments as to where I am lost and looking to accomplish...

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

if (head == null) {
    head = temp;
    cursor = temp;
    tail = temp;
    head.setNext(cursor);
    tail.setPrev(cursor);
    listCount++;
} else {
    cursor = temp;
    cursor.setPrev(head);
    cursor.setNext(tail);

    System.out.println(cursor.getPrev().getInfo().getName());
    System.out.println(cursor.getInfo().getName());
    System.out.println(cursor.getNext().getInfo().getName());
    // Now I stop here because I do not understand how to put a 3rd in
    // between my head and tail without losing the middle nodes info (cursor)
    // These printlns are here to help me understand what exactly is happening!
    // So I am rather unclear one what my next step should be
}

I AM CURRENTLY TRYING TO GET MY OTHER ATTEMPTS TO RUN WITHOUT THROWING EXCEPTIONS! WILL ADD WHEN FINISHED!

Was it helpful?

Solution

Assuming that cursor points to the node after which the node is to be inserted.

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

if(head == null){
  head = tail = cursor = tmp;
}

else{
  if(cursor == tail)
  {
    cursor.setNext(tmp);
    tmp.setPrev(cursor);
    tail = tmp;
  }
  else
  {
    tmp.setNext(cursor.getNext());
    tmp.setPrev(cursor);

    cursor.getNext().setPrev(tmp);
    cursor.setNext(tmp);
  }
}

listCount++;

With this, if the node is inserted for the first time then All (head, tail and cursor) will point to the first node. If n number of nodes are already present then we need to insert the new node according to the position of the cursor. If cursor points to the tail, then the new node is added at the end and tail is updated. If cursor points to any other node (including head) then the new node is inserted after the cursor and tail is untouched. In both the cases the head is untouched i.e., head will always point to the first node. [Tail will always point to the last node -- and updated accordingly]

Hope this helps!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top