【发布时间】:2019-03-25 17:23:40
【问题描述】:
我有一个包含 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();
}
}
}
【问题讨论】:
标签: java data-structures linked-list