7 Describe an algorithm that uses a stack to determine if th
7. Describe an algorithm that uses a stack to determine if the parentheses in a given expression are balanced. For [4*(3-5) + (15/3) *3 + 1] / {[4*6]/[(14+6)/8]}.
Solution
i am not sure this;
Algorithm:
1) Declare a character stack S.
2) Now traverse the expression string exp.
a) If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack.
b) If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced.
3) After complete traversal, if there is some starting bracket left in stack then “not balanced”
![7. Describe an algorithm that uses a stack to determine if the parentheses in a given expression are balanced. For [4*(3-5) + (15/3) *3 + 1] / {[4*6]/[(14+6)/8] 7. Describe an algorithm that uses a stack to determine if the parentheses in a given expression are balanced. For [4*(3-5) + (15/3) *3 + 1] / {[4*6]/[(14+6)/8]](/WebImages/2/7-describe-an-algorithm-that-uses-a-stack-to-determine-if-th-970922-1761496005-0.webp)