Bracket Balance using stack

In Above code why we have added top()!=’(’ this line since there is only one condition to enter data into stack that is when character is ( so whenever there is an element it will always be equal to ( .So why to add this line

you question is not clear to me
you have messed up all sentences in single line
please clearly explain your question

the algorithm of question you have asked is

Algorithm:

  • Declare a character stack S.
  • Now traverse the expression string exp.
    1. If the current character is a starting bracket ( ‘(‘ or ‘{‘ or ‘[‘ ) then push it to stack.
    2. 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.
  • After complete traversal, if there is some starting bracket left in stack then “not balanced”

My question is simple that is why we have added this line of code top()!=( there is no need to add it ??

My question is simple that is why we have added this line of code top()!=( there is no need to add it ??

if you are taking about line no 14
yes this is not neccasary
as you can’t have char other than ‘(’ in stack
you are pushing only ( braces

Yes you can remove this