我正在为一个java类开发一个实验室。以下是给出的指示和代码:给定ShoppingList类中的main((,在ItemNode类中定义一个insertAtEnd((方法,该方法将元素添加到链表的末尾。不要打印假头节点。
Ex。如果输入为:
4Kale莴苣胡萝卜花生其中4是要插入的项目数;羽衣甘蓝、生菜、胡萝卜、花生是要添加到列表末尾的项目的名称。
输出为:
Kale莴苣胡萝卜花生
public class ItemNode {
private String item;
private ItemNode nextNodeRef; // Reference to the next node
public ItemNode() {
item = "";
nextNodeRef = null;
}
// Constructor
public ItemNode(String itemInit) {
this.item = itemInit;
this.nextNodeRef = null;
}
// Constructor
public ItemNode(String itemInit, ItemNode nextLoc) {
this.item = itemInit;
this.nextNodeRef = nextLoc;
}
// Insert node after this node.
public void insertAfter(ItemNode nodeLoc) {
ItemNode tmpNext;
tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define insertAtEnd() method that inserts a node
// to the end of the linked list
// Get location pointed by nextNodeRef
public ItemNode getNext() {
return this.nextNodeRef;
}
public void printNodeData() {
System.out.println(this.item);
}
}
这也是其中的一部分,但此代码无法编辑:
import java.util.Scanner;
public class ShoppingList {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
ItemNode headNode; // Create intNode objects
ItemNode currNode;
ItemNode lastNode;
String item;
int i;
// Front of nodes list
headNode = new ItemNode();
lastNode = headNode;
int input = scnr.nextInt();
for(i = 0; i < input; i++ ){
item = scnr.next();
currNode = new ItemNode(item);
lastNode.insertAtEnd(headNode, currNode);
lastNode = currNode;
}
// Print linked list
currNode = headNode.getNext();
while (currNode != null) {
currNode.printNodeData();
currNode = currNode.getNext();
}
}
}
这就是我所拥有的,但代码没有以正确的顺序给出输出。有人能帮我理解我需要改变什么吗?
public class ItemNode {
private String item;
private ItemNode nextNodeRef; // Reference to the next node
public ItemNode() {
item = "";
nextNodeRef = null;
}
// Constructor
public ItemNode(String itemInit) {
this.item = itemInit;
this.nextNodeRef = null;
}
// Constructor
public ItemNode(String itemInit, ItemNode nextLoc) {
this.item = itemInit;
this.nextNodeRef = nextLoc;
}
// Insert node after this node.
public void insertAfter(ItemNode nodeLoc) {
ItemNode tmpNext;
tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define insertAtEnd() method that inserts a node
// to the end of the linked list
public void insertAtEnd(ItemNode headNode, ItemNode currNode){
currNode.nextNodeRef = headNode.nextNodeRef;
headNode.nextNodeRef = currNode;
// Get location pointed by nextNodeRef
public ItemNode getNext() {
return this.nextNodeRef;
}
public void printNodeData() {
System.out.println(this.item);
}
}
问题来自于在附加新节点之前需要导航到列表的最后一个节点。
你可以这样做:
public void insertAtEnd(ItemNode headNode, ItemNode currNode) {
while(headNode.nextNodeRef != null) headNode = headNode.nextNodeRef;
headNode.nextNodeRef = currNode;
}
希望这能有所帮助。如果您需要任何澄清,请随时询问。
您应该将lastNode
作为参数而不是headNode
传递,这样您就可以轻松地引用当前最后一个元素
public void insertAtEnd(ItemNode lastNode, ItemNode currNode){
lastNode.nextNodeRef = currNode;