Advertisements
Advertisements
प्रश्न
For the following arithmetic expression:
((2+3)*(4/2))+2
Show step-by-step process for matching parentheses using stack data structure.
उत्तर
Rules for matching parentheses.
- PUSH in the stack, if encounter opening parenthesis ‘(‘.
- POP from the stack, if encounter closing parenthesis ‘)’
- At the end; if stack is empty – Then Parentheses are balanced, otherwise Parentheses are unbalanced.
Given Expression : ( ( 2 + 3 ) * ( 4 / 2 ) ) + 2
Scanning from left to right –
Symbol | Stack, Initially Stack is Empty [ ] | Action |
[ ] | ||
( | ( | PUSH ‘(‘ |
( | ( ( | PUSH ‘(‘ |
2 | ( ( | Ignore |
+ | ( ( | Ignore |
3 | ( ( | Ignore |
) | ( | POP |
* | ( | Ignore |
( | ( ( | PUSH |
4 | ( ( | Ignore |
/ | ( ( | Ignore |
2 | ( ( | Ignore |
) | ( | POP |
) | [ ] | POP |
+ | [ ] | Ignore |
2 | [ ] | Ignore |
End of Symbol |
APPEARS IN
संबंधित प्रश्न
Jan Lukasiewicz in the 1920s introduced the concept of:
Arithmetic operations are not represented as:
Numbers a and b are added together and numbers c and d are also added together. The two sums are then multiplied. What is the correct way of putting it in infix?
Which data structure is commonly used for conversion of infix expression to post or prefix expression?
1 2 + 3 4 -* when written as prefix would look like:
Which of the following is a postfix operation?
Which of the following is neither infix nor prefix nor postfix?
State TRUE or FALSE for the following case:
In POSTFIX notation for expression, operators are placed after operands.
Evaluate following postfix expression while showing status of stack after the operation given A = 3, B = 5, C = 1, D = 4.
A B + C *
Evaluate following postfix expressions while showing status of stack after the operation given A = 3, B = 5, C = 1, D = 4.
A B * C / D *
What will the following expression be evaluated for in Python?
print(15.0 / 4 + (8 + 3.0))
Write the output of the code given below:
p=5
def sum(q,r=2):
global p
p=r+q**2
print(p, end= '#')
a=10
b=5
sum(a,b)
sum(r=5,q=1)
What will the following expression be evaluated for in Python?
print (4 + 3 * 5/3 − 5 % 2)