Seu SlideShare está sendo baixado. ×

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio

1 de 1 Anúncio

# Implement an algorithm using a Stack to check if an expression is corr.docx

Implement an algorithm using a Stack to check if an expression is correctly parenthesized.//Appropriate comments/documenting import java. util.*; public class ExpressionChecker {Public static Boolean checkExpressionfString exp) {Stack stack = new Stack (); for (int i = 0; i
Solution
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â€
.

Implement an algorithm using a Stack to check if an expression is correctly parenthesized.//Appropriate comments/documenting import java. util.*; public class ExpressionChecker {Public static Boolean checkExpressionfString exp) {Stack stack = new Stack (); for (int i = 0; i
Solution
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â€
.

Anúncio
Anúncio