Chapter 7 – Stacks Stacks Infix Notation Further Stack Examples Summary Slides (4 slides) Pushing/Popping a Stack Class Stack (3 slides) Using a Stack to Create a Hex # Uncoupling Stack Elt’s (6 slides) Activation Records RPN 1 Main Index Contents 2 Stacks A stack is a sequence of items that are accessible at only one end of the sequence. Main Index Contents Further Stack Examples 3 Main Index Contents 4 Pushing/Popping a Stack Because a pop removes the item last added to the stack, we say that a stack has LIFO (lastin/first-out) ordering. Main Index Contents CLASS stack Constructor <stack> Operations <stack> stack(); Create an empty stack CLASS stack bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. 5 Main Index Contents CLASS stack Operations void pop(); Remove the item from the top of the stack. Precondition: The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top. 6 Main Index Contents <stack> CLASS stack Operations <stack> int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition: The stack is not empty. const T& top() const; Constant version of top(). 7 Main Index Contents Using a Stack to Create a Hex Number '1' 'F' 431 % 16 = 15 431 / 16 = 26 '1' 'A' 'A' 'A' 'F' 'F' 'F' 26 % 16 = 10 26 / 16 = 1 1 % 16 = 1 1 / 16 = 0 'F' Pop '1' numStr = "1" Push Digit Characters 8 'A' Pop 'A' Pop 'F' numStr = "1A" numStr = "1AF" Pop Digit Characters Main Index Contents 'F' Uncoupling Stack Elements A 9 9 B Train Before Uncoupling E E C D Main Main Index Index Contents Contents Uncoupling Stack Elements A Uncouple E. Move to side track B C D E 10 Main Index Contents Uncoupling Stack Elements Uncouple D. Move to side track A B C D E 11 Main Index Contents Uncoupling Stack Elements Uncouple C Move aside A B C D E 12 Main Index Contents Uncoupling Stack Elements Attach D to end of train A B D C E 13 Main Index Contents Uncoupling Stack Elements A 14 Attach E to end of train B D E Main Index Contents C Arguments int n Return Address RetLoc or RetLoc2 Activation Record System Stack In main(): call fact(4) In fact(4): call fact(3) 15 Argument 4 Return RetLoc1 Argument 3 Return RetLoc2 Argument 4 Return RetLoc1 Main Index Contents RPN (Reverse Polish Notation) expression 2 3 + Scan of Expression and Action Current operandStack 1. Identify 2 as an operand. Push integer 2 on the stack. 2 2. Identify 3 as an operand. Push integer 3 on the stack. 3 3. Identify + as an operator Begin the process of evaluating +. 3 2 2 4. getOperands() pops stack twice and assigns 3 to right and 2 to left. operandStack empty 5. compute() evaluates left + right and returns the value 5. Return value is pushed on the stack. 16 Main Index Contents 5 Infix Expression Rules The figure below gives input precedence, stack precedence, and rank used for the operators +, -, *, /, %, and ^, along with the parentheses. Except for the exponential operator ^, the other binary operators are left-associative and have equal input and stack precedence. Precedence 17 Symbol Input precedence + * / % ^ ( ) 1 2 4 5 0 Stack precedence 1 2 3 -1 0 Main Index Contents Rank -1 -1 -1 0 0 Summary Slide 1 §- Stack - Storage Structure with insert (push) and erase (pop) operations occur at one end, called the top of the stack. - The last element in is the first element out of the stack, so a stack is a LIFO structure. 18 Main Index Contents Summary Slide 2 §- Recursion - The system maintains a stack of activation records that specify: 1) the function arguments 2) the local variables/objects 3) the return address - The system pushes an activation record when calling a function and pops it when returning. 19 Main Index Contents Summary Slide 3 §- Postfix/RPN Expression Notation - places the operator after its operands - easy to evaluate using a single stack to hold operands. - The rules: 1) Immediately push an operand onto the stack. 2) For a binary operator, pop the stack twice, perform the operation, and push the result onto the stack. 3) 20 At the end a single value remains on the stack. This is the value of the expression. Main Index Contents Summary Slide 4 §- Infix notation - A binary operator appears between its operands. - More complex than postfix, because it requires the use of operator precedence and parentheses. - In addition, some operators are left-associative, and a few are right-associative. 21 Main Index Contents