The best data structure to check whether an arithmetic
Asked by admin @ in Computer Science viewed by 348 People
Which data structure is used to check whether an arithmetic expression has balanced parentheses?
Asked by admin @ in Computer Science viewed by 348 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 379 persons
Asked by admin @ in Biology viewed by 311 persons
Asked by admin @ in Mathematics viewed by 435 persons
Asked by admin @ in Economy viewed by 375 persons
Asked by admin @ in Science viewed by 3896 persons
Asked by admin @ in Computer Science viewed by 334 persons
Asked by admin @ in Computer Science viewed by 348 persons
Asked by maham237 @ in Business viewed by 412 persons
Asked by maham237 @ in Mathematics viewed by 388 persons
Asked by maham237 @ in Mathematics viewed by 395 persons
Asked by maham237 @ in Mathematics viewed by 333 persons
Asked by admin @ in Computer Science viewed by 339 persons
Asked by admin @ in Computer Science viewed by 261 persons
Asked by admin @ in Math viewed by 340 persons
Asked by admin @ in Math viewed by 296 persons
Asked by admin @ in Computer Science viewed by 19019 persons
Asked by admin @ in Computer Science viewed by 15806 persons
Asked by admin @ in Computer Science viewed by 10971 persons
Asked by admin @ in Computer Science viewed by 10401 persons
Asked by admin @ in Computer Science viewed by 8135 persons
Asked by admin @ in Computer Science viewed by 7841 persons
Asked by admin @ in Computer Science viewed by 6486 persons
Asked by admin @ in Computer Science viewed by 5788 persons
Asked by admin @ in Computer Science viewed by 5691 persons
Asked by admin @ in Computer Science viewed by 5474 persons
Asked by admin @ in Computer Science viewed by 4980 persons
Asked by admin @ in Computer Science viewed by 4970 persons
Asked by admin @ in Computer Science viewed by 4682 persons
Asked by admin @ in Computer Science viewed by 4487 persons