Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.

Similar presentations


Presentation on theme: "Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass."— Presentation transcript:

1 Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass Stack (3 slides) Class Stack Using a Stack to Create a Hex # Using a Stack to Create a Hex # Uncoupling Stack Elt’sUncoupling Stack Elt’s (6 slides) Uncoupling Stack Elt’s Activation Records Activation Records RPN Chapter 7 – Stacks Infix Notation Infix Notation Summary SlidesSummary Slides (4 slides) Summary Slides

2 Main Index Contents 2 Stacks A stack is a sequence of items that are accessible at only one end of the sequence.

3 Main Index Contents 33 Main Index Contents Further Stack Examples

4 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 (last- in/first-out) ordering.

5 Main Index Contents 55 Main Index Contents CLASS stack Constructor stack(); Create an empty stack CLASS stack Operations bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise.

6 Main Index Contents 66 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.

7 Main Index Contents 77 Main Index Contents CLASS stack Operations 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().

8 Main Index Contents 88 Main Index Contents Using a Stack to Create a Hex Number

9 Main Index Contents 99 Main Index Contents Uncoupling Stack Elements

10 Main Index Contents 10 Main Index Contents Uncoupling Stack Elements

11 Main Index Contents 11 Main Index Contents Uncoupling Stack Elements

12 Main Index Contents 12 Main Index Contents Uncoupling Stack Elements

13 Main Index Contents 13 Main Index Contents Uncoupling Stack Elements

14 Main Index Contents 14 Main Index Contents Uncoupling Stack Elements

15 Main Index Contents 15 Main Index Contents

16 Main Index Contents 16 Main Index Contents

17 Main Index Contents 17 Main Index Contents 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 SymbolInput precedence Stack precedence Rank + -1 1 * / %2 2 ^4 3 (5 0 )0 0 0

18 Main Index Contents 18 Main Index Contents 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.

19 Main Index Contents 19 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.

20 Main Index Contents 20 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) At the end a single value remains on the stack. This is the value of the expression.

21 Main Index Contents 21 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.


Download ppt "Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass."

Similar presentations


Ads by Google