Stacks 12 example use in c arraystackint a a size 0

Info icon This preview shows pages 12–18. Sign up to view the full content.

View Full Document Right Arrow Icon
Stacks 12 Example use in C++ ArrayStack<int> A; // A = [ ], size = 0 A.push(7); // A = [7*], size = 1 A.push(13); // A = [7, 13*], size = 2 cout << A.top() << endl; A.pop(); // A = [7*], outputs: 13 A.push(9); // A = [7, 9*], size = 2 cout << A.top() << endl; // A = [7, 9*], outputs: 9 cout << A.top() << endl; A.pop(); // A = [7*], outputs: 9 ArrayStack<string> B(10); // B = [ ], size = 0 B.push("Bob"); // B = [Bob*], size = 1 B.push("Alice"); // B = [Bob, Alice*], size = 2 cout << B.top() << endl; B.pop(); // B = [Bob*], outputs: Alice B.push("Eve"); // B = [Bob, Eve*], size = 2 ©  2010 Goodrich, Tamassia * indicates top
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 13 Parentheses Matching Each “(”, “{”, or “[” must be paired with a  matching “)”, “}”, or “[” correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])} incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: ( ©  2010 Goodrich, Tamassia
Image of page 13
Stacks 14 Parentheses Matching Algorithm Algorithm ParenMatch( X,n ): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i= 0 to n- 1 do if X [ i ] is an opening grouping symbol then S .push( X [ i ]) else if X [ i ] is a closing grouping symbol then if S .empty() then return false {nothing to match with} if S .pop() does not match the type of X [ i ] then return false {wrong type} if S .empty() then return true {every symbol matched} else return false {some symbols were never matched} ©  2010 Goodrich, Tamassia
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
©  2010 Stallmann Stacks 15 Evaluating Arithmetic  Expressions 14 – 3 * 2 + 7 = (14 – (3 * 2) ) + 7  Operator precedence  * has precedence over +/– Associativity operators of the same precedence group evaluated from left to right Example: (x – y) + z rather than x – (y + z) Idea:   push each operator on the stack, but first pop and  perform higher and  equal  precedence operations. Slide by Matt Stallmann  included with permission. Slide by Matt Stallmann  included with permission.
Image of page 15
Algorithm for  Evaluating Expressions ©  2010 Stallmann 16 Stacks Slide by Matt Stallmann  included with permission. Slide by Matt Stallmann  included with permission.
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
©  2010 Stallmann Stacks 17 Algorithm on an  Example Expression 14    4  –  3  *  2  +  7  Operator   has lower  precedence than +/– 14 4 * 3 14 4 2 * 3 14 4 + 2 * 3 14 4 + 6 14 4 + 14 -2 $ 7 + 14 -2 $ F $ 14 5 Slide by Matt Stallmann  included with permission.
Image of page 17
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern