Title: Stacks
1Chapter 7 Stacks
Stacks Further Stack Examples Pushing/Popping
a Stack Class Stack (3 slides) Using a Stack to
Create a Hex Uncoupling Stack Elts (6
slides) Activation Records RPN
Infix Notation Summary Slides (4 slides)
2Stacks
- A stack is a sequence of items that are
accessible at only one end of the sequence.
3Further Stack Examples
4Pushing/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(No Transcript)
6(No Transcript)
7(No Transcript)
8Using a Stack to Create a Hex Number
9Uncoupling Stack Elements
10Uncoupling Stack Elements
11Uncoupling Stack Elements
12Uncoupling Stack Elements
13Uncoupling Stack Elements
14Uncoupling Stack Elements
15(No Transcript)
16(No Transcript)
17Infix 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.
18Summary 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.
19Summary 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.
20Summary 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.
21Summary 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.