We help IT Professionals succeed at work.

evaluate an expression using a queue.

agri_amit
agri_amit asked
on
453 Views
Last Modified: 2010-04-15
Hi,

Can anybody help me to write a program in 'c' to evaluate an expression using a queue. I am looking for the solution which does not use the concept of stack.


Any help will be highly appreciable.

Thanks in advance,
Amit
Comment
Watch Question

Top Expert 2004

Commented:
Hi agri_amit,
I don't see how this is going to work. With a stack, you do all operations, until the stack contains only one element. But with a queue, you'll get all intermediate results as output.

Cheers,

Stefan
Top Expert 2004

Commented:
agri_amit,

A queue might work if you use the prefix notation for operators:

+ * 3 4 1

You'd have to pluck an operator and then the two operands, recursing when an operand is an operator.

Author

Commented:
Can u provide sample code for the same?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2004

Commented:
ankuratvb,
> without recursion
Yes, although the recursive approach is most straightforward - but, strictly spoken, recursion is a kind of stack itself.

The recursive algorithm looks like this (pseudocode):

double get_result(){
    queue_item operator=get_queue_item();

    if(is_value(operator)) return operator; // Simple case. Plainly return.

    // All other cases involve at least one level of operators -> out item is an operator, too.
   
    // Get left + right operand
    queue_item lo=get_result();
    queue_item ro=get_result();

    return lo operator ro;
}        

Author

Commented:
Yes,
ankuratvb is correct.
recursive approach itself involves stack .
Is there no way to solve it out without using stack.

Regards,
Amit
Top Expert 2004

Commented:
agri_amit,
> Is there no way to solve it out without using stack.

You need a stack-like logic somewhere to handle nested expressions. You have several levels and need to store them somewhere - that's the basic problem.

http://groups.google.co.in/groups?hl=en&lr=&ie=UTF-8&selm=c35oee02qod%40enews1.newsguy.com&rnum=9

This looks interesting, although it would need to be tested. The problem I see is that the second queue needs to deliver values exactly at the moment you need them as part of the logic. Could be tricky with nested expressions.


Stefan
Try the links,even though mine doesnt provide code but its a pretty good discussion on how
to do it.

Just a thought,Whenever u call a function,the concept of stack does come in,so how on earth would u be able to solve this without using functions.Afterall,even in a queue,u would implement ur standard push and pop functions.

I dont know about u but i think the recursion approach which is much simpler should be used.

Hi stefan,

isnt your link pointing to the same thread as mine does?(i guess it does)
:~?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.