main index contents 11 main index contents stacks further stack examples further stack examples...
Post on 22-Dec-2015
227 Views
Preview:
TRANSCRIPT
1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents
Stacks
Further Stack Examples
Pushing/Popping a Stack
Class Stack (3 slides) (3 slides)
Using a Stack to Create a Hex #
Uncoupling Stack Elt’s (6 slides) (6 slides)
Activation Records
RPN
Chapter 7 Chapter 7 – – StacksStacks
Infix Notation
Summary Slides (4 slides)
2 Main IndexMain Index ContentsContents
StacksStacks A stack is a sequence of items that are
accessible at only one end of the sequence.
3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents
Further Stack ExamplesFurther Stack Examples
4 Main IndexMain Index ContentsContents
Pushing/Popping a StackPushing/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 IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents
CLASS stack Constructor <stack>
stack();Create an empty stack
CLASS stack Operations <stack>
bool empty(); constCheck whether the stack is empty. Return true if it is empty and false otherwise.
6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents
CLASS stack Operations <stack>
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 IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents
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().
8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents
Using a Stack to Create a Hex Using a Stack to Create a Hex NumberNumber
'1 '
'A '
'F '
'A '
'F ''F '
4 3 1 % 1 6 = 1 54 3 1 / 1 6 = 2 6
2 6 % 1 6 = 1 02 6 / 1 6 = 1
1 % 1 6 = 11 / 1 6 = 0
P u s h D igit C h aract ers
'1 '
'A '
'F '
'A '
'F ' 'F '
P o p '1 'n u m St r = "1 "
P o p D igit C h aract ers
P o p 'A 'n u m St r = "1 A "
P o p 'F 'n u m St r = "1 A F "
9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
T rain B efo re U n co u p lin g EA B C D E
10 Main IndexMain Index ContentsContents10 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
U n co u p le E . M o v e t o s id e t rackA B C D
E
11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
U n co u p le D . M o v e t o s id e t rackA B C
D
E
12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
U n co u p le C M o v e as id eA B C
D
E
13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
A t t ach D t o en d o f t rainA B D C
E
14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents
Uncoupling Stack ElementsUncoupling Stack Elements
A t t ach E t o en d o f t rainA B D E C
15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents
A rg um en tsin t n
R etu rn A d d ressR etL oc o r R etL oc2
Ac ti vat i o n R e c o r d
I n m a in ( ) : c a ll f a c t ( 4 ) A rg u m en t 4 R etu rn R e t L o c 1
S y s t e m S t a c k
I n f a c t ( 4 ) :c a ll f a c t ( 3 )
A rg u m en t 3
A rg u m en t 4
R etu rn R e t L o c 2
R etu rn R e t L o c 1
16 Main IndexMain Index ContentsContents16 Main IndexMain Index ContentsContents
R P N (R e ve rs e P o lis h N o ta tio n) e xpre s s io n 2 3 +
3
2
o pe randStac k e m pty
3. Ide ntify + a s a n ope ra tor B e gin the proc e s s of e va lua ting + .
4 . ge tO pe ra nds () pops s ta c k tw ic e a nd a s s igns 3 to r ight a nd 2 to le f t.
5 . c om pute () e va lua te s le f t + r ight a nd re turns the va lue 5 . R e turn va lue is pus he d on the s ta c k .
5
1. Ide ntify 2 a s a n ope ra nd. P us h in te ge r 2 on the s ta c k .
C urre nt o pe randStac k
2
3
2
2. Ide ntify 3 a s a n ope ra nd. P us h in te ge r 3 on the s ta c k .
Sc an o f E xpre s s io n and Ac tio n
17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents
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.
PrecedenceSymbol Input
precedenceStack precedence
Rank
+ - 1 1 -1* / % 2 2 -1^ 4 3 -1( 5 -1 0) 0 0 0
18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents
Summary Slide 1Summary 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 IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents
Summary Slide 2Summary 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 IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents
Summary Slide 3Summary 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 IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents
Summary Slide 4Summary 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.
top related