0

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!

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Sherifftwinkie
  • 391
  • 1
  • 13
  • 21
  • Is there any reason you can't use an ArrayList ? – blearn Feb 21 '13 at 04:02
  • It is for a class, I am not allowed to use any data structures, it must be made manually I guess you could say, but I have never used LinkedLists like this, nor DLL for that matter. Also my texts do nothing to help me... – Sherifftwinkie Feb 21 '13 at 04:06
  • Do you wish to insert the elements at the end or in the middle??? Could you tell what is the significance of _cursor_ . I mean what does it stand for? – asifsid88 Feb 21 '13 at 04:06
  • http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java – blearn Feb 21 '13 at 04:08
  • Well originally it was suppose to be placeholder so that I could find the location it belongs in and then put my node there, but I don't understand how to link these nodes without having the same number of nodes as objects that need to be brought in and linked. Eg. I tried to add 3 items with this method, when adding the 3rd item it just wrote over my cursor (middle). I do not understand how to make a 4th and 5th and so on without just making lots of nodes to hold those locations. – Sherifftwinkie Feb 21 '13 at 04:10

1 Answers1

1

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!!

asifsid88
  • 4,631
  • 20
  • 30
  • I really find no reason of using _cursor_ if you have _tail_ to keep the track of. Else tell exactly what you are trying to achieve – asifsid88 Feb 21 '13 at 04:21
  • Well as I stated above, I do need to organize the order of these nodes by the the rfidTag element of their contained ItemInfo object. So my plan was to use cursor as a means of finding out where the node belonged in the list of the nodes and then place the node in that position between node say...x and y. – Sherifftwinkie Feb 21 '13 at 04:25