{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

5. Stacks_outside

C t 1 void pop if empty throw stackempty pop from

Info iconThis preview shows pages 11–16. Sign up to view the full content.

View Full Document Right Arrow Icon
(c), t (-1) { } void pop() { if ( empty() ) throw StackEmpty (“ Pop from empty stack ”); t--; } void push( ) { if ( size() == cap ) throw StackFull (“Push to full stack”); S[++ t] = e; } (other methods of Stack interface) © 2010 Goodrich, Tamassia
Background image of page 11

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

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

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

View Full Document Right Arrow Icon
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
Background image of page 14
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 
Background image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page11 / 21

c t 1 void pop if empty throw StackEmpty Pop from empty...

This preview shows document pages 11 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online