Question

IN JAVA One possible application of a Stack is to check for balanced parentheses or braces...

IN JAVA

One possible application of a Stack is to check for balanced parentheses or

braces in an expression. An expression is considered to be balanced if:

  •  There are an equal number of open and close parantheses; AND

  •  Every closed parenthesis seen (when parsing the expression from left to right) must be

    paired with a previously seen open parenthesis.

    Write an algorithm (not code, an algorithm) for how you would use a stack to check for balanced parentheses. Your algorithm should be specific enough that every listed step describes a single code instruction.

Homework Answers

Answer #1

In case of any query do comment. Thanks

Answer:

1.Declare a Char Stack
2.Parse the character in string expression from left to right
3. If the current character is an opening parenthesis ('(', '{'.'[') then push onto stack
4.If the current character is closing parenthesis ('(', '{'.'['), then pop a element out of the Stack
and verify the element (just popped out) with the closing parenthesis, if brackets pair matches then fine else return not a balanced parenthesis
5. After complete parse of the string if Stack is not empty then parenthesis are not balanced otherwise balanced

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT