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?

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 379 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 311 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 435 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 375 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 3896 persons

Which scenario best illustrates the implementation of datagovernance?.​

Is not the component of data structure

Asked by admin @ in Computer Science viewed by 334 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 348 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 412 persons

Which sequences are arithmetic? check all that apply.

Asked by maham237 @ in Mathematics viewed by 388 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 395 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 333 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 339 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 261 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 340 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 296 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 19019 persons


Domestic data entry operator ncert book class 9 solutions

Asked by admin @ in Computer Science viewed by 15806 persons


Information technology code 402 class 9 solutions chapter 1

Asked by admin @ in Computer Science viewed by 10971 persons



Ncert solutions for class 8 computer chapter 1 computer system

Asked by admin @ in Computer Science viewed by 10401 persons


A scrum team works on a 4 weeks sprint

Asked by admin @ in Computer Science viewed by 8721 persons


Which of the following occupies more memory in c

Asked by admin @ in Computer Science viewed by 8135 persons



A database of motor vehicles has the base entity

Asked by admin @ in Computer Science viewed by 7841 persons


The network architecture can be termed as a mcq

Asked by admin @ in Computer Science viewed by 6486 persons


Cat5 and cat6 cabling refer to which reference model layer

Asked by admin @ in Computer Science viewed by 5788 persons



Class 7 computer chapter 3 questions and answers

Asked by admin @ in Computer Science viewed by 5691 persons


Choose the correct options about usability and user experience

Asked by admin @ in Computer Science viewed by 5474 persons


Which advocates daily team meetings for coordination and integration

Asked by admin @ in Computer Science viewed by 4980 persons



Which three security features match the database security level

Asked by admin @ in Computer Science viewed by 4970 persons


Which of the dbms package is not available

Asked by admin @ in Computer Science viewed by 4682 persons


________ aid a business grow and attain its objectives

Asked by admin @ in Computer Science viewed by 4487 persons