[英]Putting text from txt file into linked list
我正在尝试将数字表达式的行标记为CS项目的链接列表。 我必须使用在上一个实验中创建的自己的链接列表。
我对行中的每个数字和运算符进行标记化,并在对它们进行标记化时将每个标记插入链表中的节点。 当我编写程序以打印每个标记化标记时,将打印每个标记。 但是,当我告诉它打印出包含每个标记作为节点的链表时,缺少一些运算符。 我不知道此行为的原因是什么。
下面是创建包含每个令牌的链接列表的方法:
public static LinkedListTest ReadInFile(String path){
File file = new File(path);
LinkedListTest list = new LinkedListTest();
try {
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()){
list.insert(st.nextToken());
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
以下是插入链表并打印其中一个的方法:
public class LinkedListTest implements LinkedList {
private Node head;
public LinkedListTest(){
head = new Node();
}
public void insert(Object x){
if (lookup(x) == false){
if (head.data == null)
head.data = x;
else{
/*
Node NewNode = new Node();
NewNode.data = x;
NewNode.next = head;
head = NewNode;
*/
//InsertLast
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
Node NewNode = new Node();
NewNode.data = x;
NewNode.next = null;
temp.next = NewNode;
}
}
}
public void printList(){
Node temp = head;
while (temp.next != null){
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.print(temp.data + " ");
}
}
我解决了 我的insert函数上的lookup子句搞砸了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.