Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用链表堆栈,读取带有后缀表达式的输入文件_Java_Linked List_Stack - Fatal编程技术网

Java 使用链表堆栈,读取带有后缀表达式的输入文件

Java 使用链表堆栈,读取带有后缀表达式的输入文件,java,linked-list,stack,Java,Linked List,Stack,我需要用链表填充堆栈。我有一个名为GenericStack的泛型类,带有常规方法。我有一个Evaluator类,它有我的主方法,我必须读取后缀表达式的输入文件。我有一个节点类来构建链表。要读取带有后缀表达式(如6 5 2 3+8*+3+*的文件,我不知道如何用该文件填充链表或如何读取它 public class GenericStack { private Node top; public GenericStack(){ top = nu

我需要用链表填充堆栈。我有一个名为GenericStack的泛型类,带有常规方法。我有一个Evaluator类,它有我的主方法,我必须读取后缀表达式的输入文件。我有一个节点类来构建链表。要读取带有后缀表达式(如6 5 2 3+8*+3+*的文件,我不知道如何用该文件填充链表或如何读取它

 public class GenericStack {
        private Node top;
        public GenericStack(){
            top = null;
        }

        public boolean isEmpty(){
            return (top == null);
        }

        public void push (Object newItem){
            top = new Node(newItem,top);
        }

        public Objectpop(){
            if(isEmpty()){
                System.out.println("Trying to pop when stack is empty.");
                return null;
            }
            else{
                Node temp = top;
                top = top.next;
                return temp.info;
            }
        }

        void popAll(){
            top = null;
        }

        public Object peek(){
            if(isEmpty()){
                System.out.println("Trying to peek when stack is empty.");
                return null;
            }
            else{
                return top.info;
            }
        }
    }

    public class Evaluator {
        public static void main(String[] args){
            GenericStack myStack = new GenericStack();
        }
    }


public class Node {
    Object info;
    Node next;

    Node(Object info, Node next){
        this.info = info;
        this.next = next;
    }

}

使用
java.util.Scanner
逐行读取文件。然后自己实现
evaluation()
方法

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Evaluator {

// Method that evaluates postfix expressions
private static int evaluate(String expression) {

    Stack<Character> stack = new Stack<>();

    // Implement evaluation algorithm here

    return 0;
}

public static void main(String[] args) throws IOException {

    // Path to file
    String path = "file.txt";

    // Reference to file
    File file = new File(path);

    // Scanner that scans the file
    Scanner scanner = new Scanner(file);

    // List that will contain lines from file
    List<String> list = new ArrayList<>();

    // Read file in memory, line by line
    while (scanner.hasNextLine()) {

        // Add each line to list
        list.add(scanner.nextLine());
    }

    int result;

    // Loop through each line in list
    for (String line : list) {

        // Evaluate postfix expression
        result = evaluate(line);

        // Display expression = result
        System.out.println(line + " " + result);
    }

}
导入java.io.File;
导入java.io.IOException;
导入java.util.*;
公共类评估员{
//计算后缀表达式的方法
私有静态整型求值(字符串表达式){
堆栈=新堆栈();
//在这里实现评估算法
返回0;
}
公共静态void main(字符串[]args)引发IOException{
//文件路径
String path=“file.txt”;
//对文件的引用
文件=新文件(路径);
//扫描文件的扫描仪
扫描仪=新扫描仪(文件);
//将包含文件中的行的列表
列表=新的ArrayList();
//逐行读取内存中的文件
while(scanner.hasNextLine()){
//将每一行添加到列表中
添加(scanner.nextLine());
}
int结果;
//循环浏览列表中的每一行
用于(字符串行:列表){
//计算后缀表达式
结果=评估(行);
//显示表达式=结果
System.out.println(行+“”+结果);
}
}
}