英文:
How can I get my next pointer of my LinkedList class without using built in function?
问题
我是程式设计的新手,正在练习 Java 编程语言。今天我在寻找我的程序解决方案时遇到了麻烦,因为我无法获取我的“next”指针,而我真的想要打印出我的最后一个值。有人可以帮助我解决这个问题并向我解释一下吗?非常感谢。以下是我的代码。
注意:我的程序输出结果是5。
public class Node {
private int data;
private Node next;
public Node (int data){
this.data = data;
}
public int getData() {
return this.data;
}
public void setNext(Node n) {
this.next = n;
}
public Node getNext() {
return this.next;
}
}
public class LinkedList {
private static Node head, next;
public LinkedList (int data) {
head = new Node (data);
}
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
public void printList() {
head.setNext(next);
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
英文:
I'm newbie in programming and I'm practicing a Java Programming Language. I was having a rough day in finding the solution of my program because I cannot get my "next" pointer and I really want to print my last value. Could someone help me to fix this and explain to me? Thank you in advance. Here's my code.
Note: The output of my program is 5.
public class Node {
private int data;
private Node next;
public Node (int data){
this.data = data;
}
public int getData() {
return this.data;
}
public void setNext(Node n) {
this.next = n;
}
public Node getNext() {
return this.next;
}
}
public class LinkedList {
private static Node head, next;
public LinkedList (int data) {
head = new Node (data);
}
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
public void printList() {
head.setNext(next);
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
答案1
得分: 0
- 在你的
printList()
方法中,你将null
设置为下一个节点; - 你的
addLast
也不起作用(你没有设置下一个节点,详见下文); - 在你的
print
方法中,你永远不应该设置节点(或进行任何逻辑修改)。打印必须只进行打印,正如名称所示,它不应包含任何副作用,修改你的数据结构。换句话说,你必须明确分离你的关注点。
在你当前的 addLast
中,你这样做:
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
这意味着当你的 temp
的下一个节点为 null 时,你永远不会添加使用你的 int
参数实例化的节点。
将 else
块更改如下:
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
//删除了两行多余的代码
}
相应地,从你的 printList()
方法中删除 head.setNext(next);
(可能也要删除不必要的 System.out.println()
语句)。
附言:我真的建议你花些时间学习链表数据结构(数据结构,而不是Java代码),因为你当前的设计显示出你需要更好地掌握它。
英文:
TL;DR:
- You set
null
as the next node in yourprintList()
method; - Your
addLast
does not work either (you do not set the next node (see details below); - You should never set the node (or do any logical alteration whatsoever) in your
print
method. Print must just print, as the name suggests, and it should not contain any side-effect, amending your data structure. That is: you have to clearly separate your concerns.
In your current addLast
, you do:
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
which means, that when your temp
's next node is null, you never add the Node you instantiate with your int
argument.
Change the else
block as follows:
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
//two redundant lines removed
}
Correspondingly, remove head.setNext(next);
(and possibly unnecessary System.out.println()
statement) from your printList()
method.
P. S. I would really recommend you to spend some time on the Linked List Data Structure (Data Structure, and not the Java code), as your current design, shows that you need to have a better grasp of it.
答案2
得分: 0
- 在addLast方法中的else块中添加以下代码:
Node temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
- 在printList方法中迭代元素时,使用以下建议:
Node temp = head;
while (temp.getNext() != null) {
System.out.println(temp.getData());
temp = temp.getNext();
}
System.out.println(temp.getData());
你可能还需要学习链表迭代算法以获得更好的理解。
英文:
I suggest following two amendment to your code.
-
with following else block in addLast method.
Node temp = head; temp.setNext(next); // this line causing the next object to be set to null all the time. commenting this line will help in making sure the follwing loop reaches to end of the list, otherwise the while loop will always exit without any iteration. while (temp.getNext() != null) { temp = temp.getNext(); } Node t = temp.getNext(); t = n; // this will also not change the linking. Its basically assigned a new value to t.
use following suggestion
Node temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
// now we reached end of list and temp.next is null.
// assign newly createdd node to temp.next
temp.setNext(n);
- While iterating the element in printList same problem exist as mentioned in point 1. try to use following suggestion for printList method.
<pre><code>
// head.setNext(next); // This line will always set head.next to null and whole list will be lost. Instead of this use following line
Node temp = head;
while (temp.getNext() != null) { // here if you use head its position will move to end. So use temp variable for iteration
System.out.println(temp.getData());
temp= temp.getNext();
}
System.out.println(temp.getData());
</code></pre>
You may also need to study list iteration algorithm to have better understanding.
答案3
得分: 0
我已经对代码进行了一些修改以使其正常工作。在你的addLast方法中,If语句部分:
if (head == null) {
是多余的,因为你的LinkedList只能通过传递一些data来进行初始化,因此head永远不会是null,它将始终指向包含data的Node。
另外,在你的**printList()**方法中,这一行:
head.setNext(next);
是有问题的,它总是指向null。
public class LinkedList {
private static Node head, next;
public LinkedList(int data) {
head = new Node(data);
}
public void addLast(int data) {
Node n = new Node(data);
Node temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.println(temp.getData());
temp = temp.getNext();
}
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
英文:
I've made some amendments to make your code work. The If statement in your addLast method:
if (head == null) {
is redundant since your LinkedList can only be initialized by passing some data, hence head will never be null, it will always point to the Node containing data
Also the line
head.setNext(next);
in your printList() was problematic, it was always pointing to null
public class LinkedList {
private static Node head, next;
public LinkedList(int data) {
head = new Node(data);
}
public void addLast(int data) {
Node n = new Node(data);
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
}
public void printList() {
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论