can any one give a slight hint about the alogirthm to evaluate a prefix expression using queue.i tried a lot bt now atthe dead end,

Recommended Answers

All 4 Replies

*+abc should be treated as (a+b)*c so in a prefix expression traverse the expression until you find two operands after an operator and use that operator on that operand then go on doing the same thing for the rest of expression. If you know queue then you can obviously play around. You can even use STL for the ease of programming.

i started it with the same idea.bt after using the operator on two operand where should i keep the evaluated result.as i am using a character array to implement queue so i cant put that value to that array if it is a multidigit number which is case most of the time.then it will give garbage value.pls give an soln to this problem..

If you can make a queue of chars so can you make a queue of strings and in a string you can store the characters which form the entire number. There can be many ways to solve a problem (efficient and in efficient). you just need to choose how you would want to solve it.

You could also implement a discriminant union and make a queue of those; this would let you use doubles for the numbers.

enum QueueElementType {NUMBER, OPERATOR};

struct QueueElement 
{
    enum QueueElementType type;
    union 
    {
        double number;
        char operator;
    } element;

    struct QueueElement *next;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.