我正在尝试创建一个单链表,我可以在其中添加具有整数值和下一个值的节点。我需要能够遍历此列表,以便我可以添加列表的元素。一切都需要在一个文件中。我是java的新手,可以使用一些帮助。这是我的代码:
package LargestSum;
import java.io.*;
import java.util.Scanner;
public class LargestSum {
public static class LinkedList {
private int num;
private LinkedList node;
private LinkedList head;
private LinkedList tail;
private int listSize;
public LinkedList next;
public LinkedList(){
node = null;
num = 0;
head = null;
tail = null;
listSize = 0;
}
public void setLink(LinkedList l){
node = l;
}
public void setNum(int n){
num = n;
}
public LinkedList getNode(){
return node;
}
public int getNum(){
return num;
}
public boolean empty(){
return head == null;
}
public int getListSize(){
return listSize;
}
public void insert(int set){
LinkedList list = new LinkedList();
listSize++;
if(head == null){
head = list;
tail = head;
}
else {
tail.setLink(list);
tail = list;
}
}
}
public static void main(String[] args) throws IOException {
String fileName = "in.txt";
LinkedList list = new LinkedList();
Scanner numbers = new Scanner(new File(fileName));
while(numbers.hasNext()){
int num = numbers.nextInt();
list.insert(num);
System.out.println(num);
}
int listSize = list.getListSize();
}
}
我很感激任何类型的帮助和指示。谢谢。
答案 0 :(得分:1)
代码中的问题是每次添加元素时都要初始化链表。所以每个节点都没有相互链接。你可以做的是做两个类:1)节点和2)LinkedList
node将使用container元素表示LinkedList中的元素。 LinkedList类将具有您想要执行的所有功能。
我在您的代码中进行了更改,而不是粘贴全新的代码。这样您就可以通过比较代码来理解它们之间的区别。
//package LargestSum;
import java.io.*;
import java.util.Scanner;
public class Test {
public static class node{
int num;
node next;
public node(int num){
this.num = num;
next = null;
}
public node getNext(){
return next;
}
public void setNext(node t){
this.next = t;
}
public int getNum(){
return this.num;
}
}
public static class LinkedList {
//private int num;
//private LinkedList node;
private node head;
private node tail;
private int listSize;
//public LinkedList next;
public LinkedList(){
// node = null;
// num = 0;
head = null;
tail = null;
listSize = 0;
}
//public void setLink(LinkedList l){
// node = l;
//}
//public void setNum(int n){
// num = n;
//}
//public LinkedList getNode(){
// return node;
//}
//public int getNum(){
// return num;
//}
public boolean empty(){
return head == null;
}
public int getListSize(){
return listSize;
}
public void print(){
node traverse = head;
while(traverse!=null){
System.out.println(traverse.getNum());
traverse = traverse.getNext();
}
}
public void insert(int set){
// LinkedList list = new LinkedList();
node temp = new node(set);
listSize++;
if(head == null){
head = temp;
tail = temp;
}
else {
tail.setNext(temp);
tail = tail.getNext();
}
}
}
public static void main(String[] args) throws IOException {
// String fileName = "in.txt";
LinkedList list = new LinkedList();
Scanner numbers = new Scanner(System.in);
int x=10;
while(x >0){
int num = numbers.nextInt();
list.insert(num);
System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
list.print();
x--;
}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
list.print();
int listSize = list.getListSize();
}
}
在这里,我实施了非常基本的一个。您无法直接在此代码中添加其他功能。如果你想添加其他的,只需使用我在这里使用的这两种类,并在LinkedList类中添加函数。
如果您有任何进一步的疑问,请评论我。
谢谢,
Bhavik