5. Stacks_outside

Stacks 12 example use in c arraystackint a a size 0

• Notes
• 21

This preview shows page 12 - 18 out of 21 pages.

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

Subscribe to view the full document.

Stacks 13 Parentheses Matching Each “(”, “{”, or “[” must be paired with a  matching “)”, “}”, or “[” correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])} incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: ( ©  2010 Goodrich, Tamassia
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

Subscribe to view the full document.

©  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.
Algorithm for  Evaluating Expressions ©  2010 Stallmann 16 Stacks Slide by Matt Stallmann  included with permission. Slide by Matt Stallmann  included with permission.

Subscribe to view the full document.

©  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.
• Fall '09
• Void type, The Stack, Tamassia

{[ snackBarMessage ]}

"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern