Title: Chapter 7 Stacks
1Chapter 7 - Stacks
- Stack is an ADT.
- An ordered collection of data items that can be
accessed at only one end, called the top of the
stack. - Abstraction of ordinary stack, of books, bricks,
bills etc. - Last In - First Out
2STACK
3BASIC OPERATIONS
- Construct a stack
- Check if stack is empty
- Push Add an item to the top
- Top Retrieve the top element of the stack
- Pop Remove the top item from the stack
4IMPLEMENTATIONS
- Class will be used
- Our first implementation will use an array to
store elements and an int top. - Later will use a vector, dynamic array, and
linked nodes - Then a template
- Then use stack from STL
5Array Implementation
- The stack will grow upward from subscript
- 5 this
- 4 is
- 3 a
- 2 stack
- 1 of
- 0 strings. Top of stack is at 5
6 Class Definition
- // Stack.h
- / Stack.h provides a Stack class.
- Basic operations
- Constructor Constructs an empty stack
- empty Checks if a stack is empty
- push Modifies a stack by adding a value
at the top
7Basic Operations cont.
top Accesses the top stack value
leaves stack unchanged pop
Modifies a stack by removing the value
at the top display Displays all
the stack elements
8Class Invariant
- 1. The stack elements (if any) are stored in
- positions 0, 1, . . ., myTop of
myArray. - 2. -1 lt myTop lt STACK_CAPACITY
9Example of Stack Problem
- It is desired to find the binary representation
of an integer. One algorithm to do this is to
repeatedly divide by 2, with the remainders being
the binary representation from right to left.
10Consider the number 13
- 13 / 2 6, 13 2 1
- 6 / 2 3, 6 2 0
- 3 / 2 1, 3 2 1
- 1 / 2 0, 1 2 1
- binary representation is 1101
11Using Stack
- Put the remainders on a stack
- 3 1
- 2 1
- 1 0
- 0 1
- Pop the stack to get 1101
12Implementing Stack Class Refined
Instead of modeling a stack of plates, model a
stack of books (or a discard pile in a card game.)
Keep the bottom of stack at position 0.
Maintain a "pointer" myTop to the top of the
stack.
Note We don't clear this.
myTop?
myTop?
myTop?
myTop?
myTop?
myTop -1
myTop 0
myTop 1
myTop 2
myTop 1
13Palindrome- Word that is samespelled backward.
- MOM , ABBA, DAD, ERE are
- Desired to have an algorithm to determine if
string is palindrome. - The string can easily be reversed by pushing all
the letters on a stack and then popping them off
into another string.
14Postfix and Infix Notation
- Infix ordinary notation for expressions, e.g.2
(5 7) - Postfix many compilers translate infix to
postfix for faster evaluation, e.g.5 7 2 - Stacks can be used to translate infix to postfix
and to evaluate postfix.
15POSTFIX EXPRESSIONS
- infix postfix
- A B A B
- A B C A B C
- A - B - C A B - C -
- A B C - D / E A B C D E / -
- ( A B ) C A B C
16ADVANTAGES
- To evaluate, dont need to worry with
parenthesis. - To evaluate 3 5 2, have to KNOW that has
precedence over . - Postfix form is 3 5 2 . Operator is applied
to two preceding numbers. - Never need parenthesis. ( 3 5 ) 2 is
written as 3 5 2
17EVALUATION OF POSTFIX
- Scan tokens from left,
- if number
- push on stack
- else if operator
- pop 2 numbers from stack
- apply operator
- push result on stack
18EVALUATION CONTINUED
- After all token have been scanned, there should
be exactly one number on stack, pop it and it is
the value. - An illegal postfix expression can be detected by
there not being two numbers to pop when needed or
there being something on the stack after the
final pop is performed
19Example 3 4 2 - 5 6
- Token Stack
- 3 3
- 4 3 , 4
- 2 3 , 4 , 2
- - 3 , 2 // 4
- 2 - 5 3 , 2 , 5
- 6 3 , 2 , 5 , 6
20Example 3 4 2 - 5 6
- 6 3 , 2 , 5 , 6
- 3 , 2 , 30 //
5 6 - 3 , 32 // 2
30 - 96
- Result 96 empty
21Practice Find value of the following
- 4 4 4 4 - - -
- 5 7 2 3
- 1 8 7 - 4 4 -
22Converting infix to postfixwithout using a stack
- Order the operators by precedence, convert each
one to postorder based on that order. - ( ( 3 5 ) 4 - 2) 6
- 1 2 3 4
- ( (3 5 ) 4 - 2) 6 // converted 1
- ( ( 3 5 4 ) - 2) 6 //converted 2
- (3 5 4 2 -) 6 //converted 3
- 3 5 4 2 - 6
23Stack Algorithm for Converting to Postfix
- Scan each token from left,
- if (token ( ) push on stack
- else if token ), pop and add to postfix
until ( is popped - else if number, add to postfix
- (continued on next slide)
24Algorithm continued
- else if (token is operator)
- while (stack is not empty and token does not have
higher precedence than top of stack), pop and add
to postfix string. - push token on stack
- After all tokens, empty stack and add to postfix
25Example ( 3 ( 5 2 ) ) - 4
- Token Stack Postfix
- ( (
- 3 3
- (
- ( ( (
- 5 3 5
- ( (
26Example ( 3 ( 5 2 ) ) - 4
- ( ( 3 5
- 2 3 5 2
- ) ( 3 5 2
- ) 3 5 2
- - -
- 4 - 3 5 2
4 - cleanup 3 5 2 4
-
27PracticeShow Stack for Each
- 3 4 5 ( 7 - 3 )
- ( 6 - ( 6 - 1) - (4 2 ) ) 5
28Putting It Together
InfixToPostfix Program
Enter infix
Postfix file
EvaluatePostfix Program
Result
Postfix file
29Example
- InfixToPostfix reads Infix expression from
keyboard 7 6 5 - InfixToPostfix stores result 7 6 5
- in Postfix file
- EvaluatePostfix reads Postfix file
- EvaluatePostfix displays result 37
30Application of Stacks Run-time Stack
Whenever a function begins execution (i.e., is
activated), an activation record (or stack
frame) is created to store the current
environment for that function. Its contents
include
What kind of data structure should be used to
store these so that they can be recovered and the
system reset when the function resumes execution?
31Problem FunctionA can call FunctionB FunctionB
can call FunctionC . . . When a function calls
another function, it interrupts its own execution
and needs to be able to resume its execution in
the same state it was in when it was
interrupted. When FunctionC finishes, control
should return to FunctionB. When FunctionB
finishes, control should return to FunctionA.
So, the order of returns from a function is the
reverse offunction invocations that is, LIFO
behavior.? Use a stack to store the activation
records. Since it is manipulated at run-time, it
is called the run-time stack.
32What happens when a function is called? 1. Push
a copy of its activation record onto the run-time
stack 2. Copy its arguments into the parameter
spaces 3. Transfer control to the address of the
function's body The top activation record in the
run-time stack is always that of the function
currently executing. What happens when a
function terminates? 1. Pop activation record of
terminated function from the run-time
stack 2. Use new top activation record to
restore the environment of the interrupted
function and resume execution of the
interrupted function.
33Examples
Trace run-time stack for the following . . .int
main() . . . f2(...) f3(...)void
f1(...) . . .void f2(...) ... f1(...)
...void f3(...) ... f2(...) ...
This pushing and popping of the run-time stack is
the real overhead associated with function calls
that inlining avoids by replacing the function
call with the body of the function.