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
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT