【问题标题】:Stack using linked list, reading input file with postfix expression使用链表堆栈,使用后缀表达式读取输入文件
【发布时间】:2018-04-27 01:45:32
【问题描述】:

我需要使用链表填充堆栈。我有一个名为 GenericStack 的通用类,带有常规方法。我有一个 Evaluator 类,它有我的主要方法,我必须读取后缀表达式的输入文件。我有一个 Node 类来构建链表。要使用 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 linked-list stack


    【解决方案1】:

    使用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);
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2012-09-22
      • 2011-06-13
      • 2014-07-01
      • 2011-12-14
      • 2014-05-10
      • 1970-01-01
      相关资源
      最近更新 更多