我正在尋找使用一種方法,它接收對象的信息,創建對象的實例,設置信息,然後創建節點並將信息設置到節點上,最後將節點插入到它所屬的鏈接列表中。鏈表只能由rfidTag
String
類型組織,它是一個9位十六進制表示。以下是我迄今爲止(我忽略了「由rfidTag」部分)...插入節點到鏈接列表
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);
}
}
現在我沒有絲毫線索,放什麼做的,但我會告訴你我已經試過並添加註釋,以我在哪裏丟失,並且希望完成...
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
}
目前我正在試圖讓我的其他嘗試運行沒有出現任何例外!完成後會添加!
是否有任何理由不能使用ArrayList? –
blearn
2013-02-21 04:02:18
這是一個類,我不能使用任何數據結構,它必須手動進行我想你可以說,但我從來沒有像這樣使用LinkedLists,也沒有使用DLL。此外,我的文本沒有幫助我... – Sherifftwinkie 2013-02-21 04:06:46
你是否希望在最後或中間插入元素?你能說出_cursor_的重要性嗎?我的意思是它代表什麼? – asifsid88 2013-02-21 04:06:48