java - 可以仅使用队列将中缀表示法中的字符串转换为前缀表示法吗? (考虑唯一的操作是 + 和 - 的情况)

标签 java string queue prefix infix-notation

public class Convert{ 
/* the algorithm works as follows after inserting all elements of infix string 
into an empty Queue iterate over queue for infix.length() number of times and 
check if element at front of queue is an operator if yes enque the element to 
back of the queue else deque the operand and concatenate it with the empty 
prefix string here is an example then finally deque the queue elements with 
the prefix string*/                  

    public static String Pre(String in){
        int i = 0;
        Queue Q = new Queue(in.length());     //assuming a Queue of characters
        while(i<in.length()){
            Q.enque(in.charAt(i));       
        }
        i = in.length()-1;
        String pre = "";
        while(i>=0){                         //move operators to end of queue
            if(Q.peek()=='+'||'-'){           //if character is oper enque at end 
                Q.enque(Q.deque);}
            else{
                pre  = pre + Q.deque;
            }            // concatenate operands with prefix 
        }
        while(!Q.isEmpty) {                        //concatenate the operators 
            pre = Q.deque + pre;
        }
        return pre;                               // end of method
    }
}

最佳答案

一个“PostProduction System仅使用队列。它具有与图灵机相当的能力。

关于java - 可以仅使用队列将中缀表示法中的字符串转换为前缀表示法吗? (考虑唯一的操作是 + 和 - 的情况),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867256/

相关文章:

iphone - 当 NSOperationQueue 完成所有任务时获取通知

java - WEKA:从classifyInstance获取类,为什么这是错误的

java - 如何使用 opencsv 读取包含 '\' 的字符串?

java - 将值分配给 String 类而不创建新实例

ruby - 如何从字符串中删除不可打印的不可见字符?

java - 如何在Java上正确编写RabbitMQ Publisher?

java - android java上下文解释

java - java语句的执行顺序

windows - 每 x 个字符批量添加一个字符

generics - 不安全队列实现