stack_queue_application

stack_queue_application - An Application of Stacks and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: An Application of Stacks and Queues 1 Algebraic Expressions (infix, postfix, and prefix): This week we discuss a major application of stacks. Though it is one of the well known applications, it is by no means the only one. To proceed with the presentation of our problem and the development of algorithms and programs to solve the problem, it is necessary to provide some background. Consider the sum of A and B. We think of applying the operator + to the operands A and B and express the sum as A+B. This particular representation is called infix . There are two alternative notations for expressing the sum of A and B using the symbols A, B, and +. These are +AB prefix representation AB+ postfix representation The prefixes pre , post and in refer to the relative position of the operator with respect to the two operands. In prefix notation the operator precedes the two operands, in postfix notation the operator follows the two operands, and in infix notation the operator is between the two operands. Consider the expression A+B*C, as written in standard infix notation. To evaluate A+B*C, one requires knowledge of which of the two operations, + or *, is to be performed first. In the case of + and * we know, from our knowledge of evaluating algebraic expressions, that multiplication is to be done before addition. Thus A+B*C is to be interpreted as A+(B*C) unless otherwise specified. We say that multiplication takes precedence over addition. Suppose that we would now like to rewrite A+B*C in postfix. Applying the rules of precedence, we first convert the portion of the expression that is evaluated first, namely the multiplication B*C. We do this conversion in stages. Converting B*C first to postfix, the expression A+B*C becomes, underline for emphasis, A+BC* . Next we convert the the addition to obtain the final expression ABC*+ that is the postfix form of our original infix expression A+B*C. The only rules to remember during the conversion process is that the operations with highest precedence are converted first and that after a portion of the expression has been converted to postfix it is to be treated as a single operand. Let us now consider the same example with the precedence of operators reversed by the deliberate insertion of parentheses. Consider the expression (A+B)*C in infix form. First convert the addition in (A+B )*C to obtain AB+ *C. Then convert the multiplication in the expression AB+ *C to get the final expression AB+C* in postfix form. In the example above, the addition was converted before the multiplication because of the parentheses. In going from (A+B)*C to AB+ *C, A and B are the operands and + is the operator. In going from AB+ *C to AB+C*, AB+ and C are the operands and * is the operator. The rules An Application of Stacks and Queues 2 for converting from infix to postfix are simple provided that you know the order of precedence....
View Full Document

Page1 / 8

stack_queue_application - An Application of Stacks and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online