22. Circular Implementation bool Queue::remove(int &n) { if (! isEmpty() { n = QueueArray[front]; front++; if (front == maxSize) front = 0; size--; return true; } else return false; } 0 1 2 3 MS-1
23.
24.
25.
26.
27.
28.
29.
30.
31. Evaluation of Fully Parenthesized Expression (a+(b/c)) Assuming a=2, b=6, c=3 Pop”(a+2” and evaluate and push the result back 4 ) Pop”(b/c” and evaluate and push the result back (a+2 ) Push (a+(b/c c push (a+(b/ / push (a+(b b push (a+( ( push (a+ + push (a a Push ( ( Remarks Stack Input Symbol
35. Algorithm to Evaluate Expressions in RPN (a+b)*(c+d) ab+cd+* Assuming a=2, b=6, c=3, d=-1 Pop 8 and 2 from the stack, multiply, and push the result back. Since this is end of the expression, hence it is the final result. 16 * Pop c and d from the stack, add, and push the result back 8 2 + Push 8 c d d Push 8 c c Pop a and b from the stack, add, and push the result back 8 + Push a b b Push a a Remarks Stack Input Symbol
36.
37. Converting Infix to RPN (a+b)*(c+d) ab+cd+* Pop remaining symbols from the stack and display RPN a b + c d + * End of input Pop till “(” is found and display – RPN a b + c d + * ) Operand – display – RPN a b + c d * ( + d Push as + has higher precedence than ( * ( + + Operand – display – RPN a b + c * ( c Push * ( ( Push as stack is empty * * Pop till “(” is found and display – RPN a b + ) Operand – display – RPN a b ( + b Push as + has higher precedence than ( ( + + Operand – display – RPN a ( a Push ( ( Remarks Stack Input Symbol
38. a+b*c/(d+e) a b c * d e + / + Pop till “(” is found – RPN a b c * d e + + / ) Pop remaining symbols from the stack and display RPN a b c * d e + / + End of input Operand – display – RPN a b c * d e + / ( + e Push as + has higher precedence than ( + / ( + + Operand – display – RPN a b c * d + / ( d Push + / ( ( Pop * and push / as * and / have the same precedence but / has higher precedence than + – RPN a b c * + / / Operand – display – RPN a b c + * c Push as * has higher precedence than + + * * Operand – display – RPN a b + b Push as stack is empty + + Operand – display – RPN a a Remarks Stack Input Symbol