我有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);
*/
theLinkedList.display();
System.out.println("Value of first in LinkedList " + theLinkedList.firstLink + "\n");
// Removes the last Link entered
theLinkedList.removeFirst();
theLinkedList.display();
//System.out.println(theLinkedList.find("The Lord of the Rings").bookName + " Was Found");
//theLinkedList.removeNodeList("A Tale of Two Cities");
System.out.println("\nA Tale of Two Cities Removed\n");
theLinkedList.display();
}
}
public class LinkList {
// Reference to first Link in list
// The last Link added to the LinkedList
public NodeList firstLink;
LinkList(){
// Here to show the first Link always starts as null
firstLink = null;
}
// Returns true if LinkList is empty
public boolean isEmpty(){
return(firstLink == null);
}
public void insertFirstLink(int nodeid, int nodestate, int x_cord, int y_cord, int direction){
NodeList newLink = new NodeList(nodeid, nodestate, x_cord, y_cord, direction);
// Connects the firstLink field to the new Link
newLink.next = firstLink;
firstLink = newLink;
}
public NodeList removeFirst(){
NodeList linkReference = firstLink;
if(!isEmpty()){
// Removes the Link from the List
firstLink = firstLink.next;
} else {
System.out.println("Empty LinkedList");
}
return linkReference;
}
public NodeList removeNodeList(){
NodeList linkReference = firstLink;
if(!isEmpty()){
// Removes the Link from the List
firstLink = firstLink.next;
} else {
System.out.println("Empty LinkedList");
}
return linkReference;
}
public void display(){
NodeList theLink = firstLink;
// Start at the reference stored in firstLink and
// keep getting the references stored in next for
// every Link until next returns null
while(theLink != null){
theLink.display();
System.out.println("Next Link: " + theLink.next);
theLink = theLink.next;
System.out.println();
}
}
}
答案 0 :(得分:0)
仅当节点列表中的元素数相同时,才可以执行此操作。 作为单节点,请使用仅声明一次的struct,以便确定其内存。因此,您无法在运行时增加节点的大小。 下面我提到了C ++代码。
struct Node{
int list[5];
Node *node;
}
答案 1 :(得分:0)
您可以执行此操作。 将临时节点指向头节点,然后进行迭代。
System.out.println(temp.your_element_name);