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?

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;

}


Similar Questions

Which of the following best defines the term secondary data

Asked by admin @ in History viewed by 389 persons

Which of the following is the best definition for secondary source? O A. A timeline describing the major parts of an event O B. A description of an event written …

What is the best graph to use for qualitative data

Asked by admin @ in Biology viewed by 316 persons

Both qualitative and quantitative data can be displayed using a _____ graph circle line bar pie

Which type of data is best displayed in a histogram

Asked by admin @ in Mathematics viewed by 444 persons

Which type of data would be BEST displayed in a histogram? A. The number of fluoride treatments given to students each month for the last 60 months (range: 40-620) B. …

For dealing with qualitative data the best average is

Asked by admin @ in Economy viewed by 384 persons

Which is the most suitable average for qualitative measurement?

Which scenario best illustrates the implementation of data governance

Asked by admin @ in Science viewed by 3906 persons

Which scenario best illustrates the implementation of datagovernance?.​

Is not the component of data structure

Asked by admin @ in Computer Science viewed by 342 persons

Choose the correct option--- 1) __________________is NOT the component of data structure. a) Operations c) Algorithms b) Storage Structures d)None of above 2) Which of the following aretrue about the …

What is the best neural network model for temporal data

Asked by admin @ in Computer Science viewed by 359 persons

What is the best neural network model for temporal data?

Which terms best describe sales tax check all that apply

Asked by maham237 @ in Business viewed by 422 persons

Which sequences are arithmetic? check all that apply.

Asked by maham237 @ in Mathematics viewed by 394 persons

Which sequences are arithmetic? Check all that apply. –5, 5, –5, 5, –5, … 96, 48, 24, 12, 6 18, 5.5, –7, –19.5, –32, … –1, –3, –9, –27, –81, …

You use a line of best fit for a set of data

Asked by maham237 @ in Mathematics viewed by 402 persons

You use a line best fit for a set of data to make a prediction about an unknown value. The correlation coefficient for your data set is -0.015. How confident …

What exponential function is the best fit for the data in the table?

Asked by maham237 @ in Mathematics viewed by 338 persons

x f(x) 3 10 4 16 5 28 A) f(x) = 3(2)x − 2 + 4 B) f(x) = 3(2)x − 2 − 4 C) f(x) = 1/3(2)x − 2 …

The structure or format of data is called

Asked by admin @ in Computer Science viewed by 347 persons

5. The structure or format of data is called​

Data structure suitable for the application is discussed in

Asked by admin @ in Computer Science viewed by 267 persons

Data structure suitable for an application discussed in? A) procedural design B) architectural design C) data design D) interface design

Which measure of central tendency best describes the data

Asked by admin @ in Math viewed by 345 persons

Which measure of central tendency best describes the data and why

Check whether 150 is a term of the ap

Asked by admin @ in Math viewed by 299 persons

Check whether − 150 is a term of the A.P. 11, 8, 5, 2, …

Most viewed questions in Computer Science


A programmer mistakenly writes gor instead of for

Asked by admin @ in Computer Science viewed by 19037 persons


Domestic data entry operator ncert book class 9 solutions

Asked by admin @ in Computer Science viewed by 15825 persons


Information technology code 402 class 9 solutions chapter 1

Asked by admin @ in Computer Science viewed by 10983 persons



Ncert solutions for class 8 computer chapter 1 computer system

Asked by admin @ in Computer Science viewed by 10415 persons


A scrum team works on a 4 weeks sprint

Asked by admin @ in Computer Science viewed by 8729 persons


Which of the following occupies more memory in c

Asked by admin @ in Computer Science viewed by 8153 persons



A database of motor vehicles has the base entity

Asked by admin @ in Computer Science viewed by 7852 persons


The network architecture can be termed as a mcq

Asked by admin @ in Computer Science viewed by 6500 persons


Cat5 and cat6 cabling refer to which reference model layer

Asked by admin @ in Computer Science viewed by 5803 persons



Class 7 computer chapter 3 questions and answers

Asked by admin @ in Computer Science viewed by 5705 persons


Choose the correct options about usability and user experience

Asked by admin @ in Computer Science viewed by 5486 persons


Which advocates daily team meetings for coordination and integration

Asked by admin @ in Computer Science viewed by 4993 persons



Which three security features match the database security level

Asked by admin @ in Computer Science viewed by 4984 persons


Which of the dbms package is not available

Asked by admin @ in Computer Science viewed by 4697 persons


________ aid a business grow and attain its objectives

Asked by admin @ in Computer Science viewed by 4498 persons