我有一個包含n個節(jié)點的鏈接列表,每個節(jié)點包含多個元素。我正在嘗試編寫一個允許我搜索節(jié)點的方法和另一個允許我在節(jié)點內(nèi)搜索元素的方法。我不知道我應(yīng)該如何訪問鏈表節(jié)點的內(nèi)部元素。所以我想我真正想知道的是,你如何引用/訪問每個單獨的元素與一個鏈表的節(jié)點?嘗試創(chuàng)建一個允許創(chuàng)建鏈接列表的程序,其中該鏈接列表中的節(jié)點數(shù)取決于用戶。該列表應(yīng)允許搜索節(jié)點和元素,并且還應(yīng)該進(jìn)行排序。 package nodelist; public class NodeList {public int nodeid;public int nodestate;public int x_cord;public int y_cord;public int direction;public NodeList next;public NodeList(int nodeid, int nodestate, int x_cord, int y_cord, int direction){ this.nodeid = nodeid; this.nodestate = nodestate; this.x_cord = x_cord; this.y_cord = y_cord; this.direction = direction;}public void display(){ System.out.println("nodeid: "+nodeid + " state: " +nodestate+ " x: " +x_cord+ " y: " +y_cord+ " direction: " +direction);}//@Overridepublic String toString(){ return String.valueOf(this.nodeid); // Needed to convert int nodeid to string for printing}public static void main(String[] args) { // TODO code application logic here LinkList theLinkedList = new LinkList(); // Insert Link and add a reference to the book Link added just prior // to the field next System.out.println("Enter the number of nodes to deploy"); int nodecount = 5; int nodeid = 5000; for(int i=0; i<nodecount;i++){ theLinkedList.insertFirstLink(nodeid, 0,0,0,0); nodeid++; } /* theLinkedList.insertFirstLink("5000", 0,0,0,0); theLinkedList.insertFirstLink("5001", 1,1,1,1); theLinkedList.insertFirstLink("5002", 2,2,2,2); theLinkedList.insertFirstLink("5003", 3,3,3,3); }
2 回答

慕標(biāo)琳琳
TA貢獻(xiàn)1830條經(jīng)驗 獲得超9個贊
簡單地說,你可以做到這一點。將臨時節(jié)點指向頭節(jié)點,然后迭代溫度。
System.out.println(temp.your_element_name);

aluckdog
TA貢獻(xiàn)1847條經(jīng)驗 獲得超7個贊
僅當(dāng)節(jié)點列表中的元素數(shù)相同時,才能執(zhí)行此操作。作為單節(jié)點,請使用僅聲明一次的結(jié)構(gòu),因此它的內(nèi)存已經(jīng)確定。因此,您無法在運(yùn)行時增加節(jié)點的大小。下面我提到了C++代碼。
struct Node{
int list[5];
Node *node;
}
添加回答
舉報
0/150
提交
取消