英文:
why it doesn't print ? LinkedList
问题
i have a problem that the method in LinkedList class don't print anything.. and i'm trying hard to know what's the problem i hope someone help
the main class
public static void main(String[] args) {
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(50);
LinkedList List1 = new LinkedList();
List1.printAllNodes();
}
}
LinkedList class
public class LinkedList {
private LLnode head;
public LLnode gethead() {
return this.head;
}
public void sethead(LLnode LLnode) {
this.head = LLnode;
}
// Constructor
public LinkedList() {
head = null;
}
// Example Method to check if list is empty
public boolean isEmpty() {
return head == null;
}
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
System.out.print(helpPtr.getdata() + " ");
helpPtr = helpPtr.getnext();
}
英文:
i have a problem that the method in LinkedList class don't print anything.. and i'm trying hard to know what's the problem i hope someone help
the main class
public static void main(String[] args) {
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(50);
LinkedList List1 = new LinkedList();
List1.printAllNodes();
}
}
LinkedList class
public class LinkedList {
private LLnode head;
public LLnode gethead() {
return this.head;
}
public void sethead(LLnode LLnode) {
this.head = LLnode;
}
// Constructor
public LinkedList() {
head = null;
}
// Example Method to check if list is empty
public boolean isEmpty() {
return head == null;
}
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
System.out.print(helpPtr.getdata() + " ");
helpPtr = helpPtr.getnext();
}
why it dosn't print i tried so hard
答案1
得分: 4
这是因为您从未向您的链表中添加任何节点。
代码可能如下所示。请注意,节点将在列表开头添加。
主类:
public static void main(String[] args) {
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(50);
LinkedList List1 = new LinkedList();
List1.add(a).add(b).add(c);
List1.printAllNodes();
}
链表类:
public class LinkedList {
private LLnode head;
public LLnode gethead() {
return this.head;
}
public void sethead(LLnode LLnode) {
this.head = LLnode;
}
// 构造函数
public LinkedList() {
head = null;
}
// 示例方法以检查列表是否为空
public boolean isEmpty() {
return head == null;
}
public LinkedList add(LLnode node){
LLnode oldHead = this.head;
this.head = node;
node.setNext(oldHead);
return this;
}
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
System.out.print(helpPtr.getdata() + " ");
helpPtr = helpPtr.getnext();
}
}
}
注意:上述代码中的注释已经进行了翻译。
英文:
This is because you never add any nodes to your LinkedList.
Code could be as follows. Beware, nodes will be added at the beginning of list.
Main class:
public static void main(String[] args) {
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(50);
LinkedList List1 = new LinkedList();
List1.add(a).add(b).add(c);
List1.printAllNodes();
}
}
LinkedList class:
public class LinkedList {
private LLnode head;
public LLnode gethead() {
return this.head;
}
public void sethead(LLnode LLnode) {
this.head = LLnode;
}
// Constructor
public LinkedList() {
head = null;
}
// Example Method to check if list is empty
public boolean isEmpty() {
return head == null;
}
public LinkedList add(LLnode node){
LLnode oldHead = this.head();
this.head = node;
node.setNext(oldHead);
return this;
}
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
System.out.print(helpPtr.getdata() + " ");
helpPtr = helpPtr.getnext();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论