The best data structure to check whether an arithmetic
Asked by admin @ in Computer Science viewed by 353 People
Which data structure is used to check whether an arithmetic expression has balanced parentheses?
Asked by admin @ in Computer Science viewed by 353 People
Which data structure is used to check whether an arithmetic expression has balanced parentheses?
Answered by admin @
There are three types of parentheses [ ] { } (). Below is an arbit c code segment which has parentheses of all three types.
void func(int c, int a[])
{
return ((c +2) + arr[(c-2)]) ;
}
Stack is a straightforward choice for checking if left and right parentheses are balanced. Here is a algorithm to do the same.
/*Return 1 if expression has balanced parentheses */
bool areParenthesesBalanced(expression )
{
for each character in expression
{
if(character == ’(’ || character == ’{’ || character == ’[’)
push(stack, character);
if(character == ’)’ || character == ’}’ || character == ’]’)
{
if(isEmpty(stack))
return 0; /*We are seeing a right parenthesis
without a left pair*/
/* Pop the top element from stack, if it is not a pair
bracket of character then there is a mismatch.
This will happen for expressions like {(}) */
else if (! isMatchingPair(pop(stack), character) )
return 0;
}
}
if(isEmpty(stack))
return 1; /*balanced*/
else
return 0; /*not balanced*/
} /* End of function to check parentheses */
/* Returns 1 if character1 and character2 are matching left
and right parentheses */
bool isMatchingPair(character1, character2)
{
if(character1 == ‘(‘ && character2 == ‘)’)
return 1;
else If(character1 == ‘{‘ && character2 == ‘}’)
return 1;
else If(character1 == ‘[‘ && character2 == ‘]’)
return 1;
else
return 0;
}
Asked by admin @ in History viewed by 389 persons
Asked by admin @ in Biology viewed by 316 persons
Asked by admin @ in Mathematics viewed by 444 persons
Asked by admin @ in Economy viewed by 384 persons
Asked by admin @ in Science viewed by 3906 persons
Asked by admin @ in Computer Science viewed by 342 persons
Asked by admin @ in Computer Science viewed by 359 persons
Asked by maham237 @ in Business viewed by 422 persons
Asked by maham237 @ in Mathematics viewed by 394 persons
Asked by maham237 @ in Mathematics viewed by 402 persons
Asked by maham237 @ in Mathematics viewed by 338 persons
Asked by admin @ in Computer Science viewed by 347 persons
Asked by admin @ in Computer Science viewed by 267 persons
Asked by admin @ in Math viewed by 345 persons
Asked by admin @ in Math viewed by 299 persons
Asked by admin @ in Computer Science viewed by 19037 persons
Asked by admin @ in Computer Science viewed by 15825 persons
Asked by admin @ in Computer Science viewed by 10983 persons
Asked by admin @ in Computer Science viewed by 10415 persons
Asked by admin @ in Computer Science viewed by 8153 persons
Asked by admin @ in Computer Science viewed by 7852 persons
Asked by admin @ in Computer Science viewed by 6500 persons
Asked by admin @ in Computer Science viewed by 5803 persons
Asked by admin @ in Computer Science viewed by 5705 persons
Asked by admin @ in Computer Science viewed by 5486 persons
Asked by admin @ in Computer Science viewed by 4993 persons
Asked by admin @ in Computer Science viewed by 4984 persons
Asked by admin @ in Computer Science viewed by 4697 persons
Asked by admin @ in Computer Science viewed by 4498 persons