【发布时间】:2020-11-26 20:09:37
【问题描述】:
下面是一个示例测试问题,我完全迷失了,不知道如何回答。
“给你一个文本文件,其中包含一系列按升序排列的整数。编写一个完整的程序,将文件的内容以相反的顺序打印出来。你必须使用链表来保存文件的内容,并且您必须定义自己的链表类。不要使用 Java API LinkedList 实现。"
我的问题是:
我有以下(工作)代码,但这个定义是否满足上述问题的要求?如果是的话,我将如何使用它来打印整数。如果否,我必须添加什么替换才能使其工作?
public class Node {
String value;
Node next;
public Node(String s) {
this.value = s;
next = null;
}
public Node(String s, Node n) {
this.value = s;
this.next = n;
}
public void setValue(String newValue) { //setters
this.value = newValue;
}
public void setNext(Node newNext) {
this.next = newNext;
}
public String getValue() { // getters
return this.value;
}
public Node getNext() {
return this.next;
}
}
【问题讨论】:
-
嗯,它不打印任何东西,不读取文件,也不实现链表,所以它没有完成任务,但它可能可以用作它的一部分。
标签: java linked-list singly-linked-list