0
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
    }
}
4

2 回答 2

1

后期制作系统仅使用队列。它具有与图灵机相当的能力。

于 2011-10-23T17:56:25.677 回答
0

鉴于您指定的语法非常有限,在我看来,您可以使用双端队列将中缀转换为前缀。如果语法更复杂,我怀疑这种方法是否可行。

于 2011-10-23T16:31:54.810 回答