我有一个包含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);
}
//@Override
public 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);
}
慕标琳琳
aluckdog
相关分类