Chapter 7 Stacks - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Chapter 7 Stacks

Description:

Palindrome- Word that is same. spelled backward. MOM , ABBA, DAD, ERE are. Desired to have an algorithm to determine if string is palindrome. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 34
Provided by: jha47
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Stacks


1
Chapter 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

2
STACK
3
BASIC 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

4
IMPLEMENTATIONS
  • 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

5
Array 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

7
Basic 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
8
Class Invariant
  • 1. The stack elements (if any) are stored in
  • positions 0, 1, . . ., myTop of
    myArray.
  • 2. -1 lt myTop lt STACK_CAPACITY

9
Example 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.

10
Consider 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

11
Using Stack
  • Put the remainders on a stack
  • 3 1
  • 2 1
  • 1 0
  • 0 1
  • Pop the stack to get 1101

12
Implementing 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
13
Palindrome- 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.

14
Postfix 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.

15
POSTFIX 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

16
ADVANTAGES
  • 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

17
EVALUATION 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

18
EVALUATION 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

19
Example 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

20
Example 3 4 2 - 5 6
  • 6 3 , 2 , 5 , 6
  • 3 , 2 , 30 //
    5 6
  • 3 , 32 // 2
    30
  • 96
  • Result 96 empty

21
Practice Find value of the following
  • 4 4 4 4 - - -
  • 5 7 2 3
  • 1 8 7 - 4 4 -

22
Converting 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

23
Stack 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)

24
Algorithm 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

25
Example ( 3 ( 5 2 ) ) - 4
  • Token Stack Postfix
  • ( (
  • 3 3
  • (
  • ( ( (
  • 5 3 5
  • ( (

26
Example ( 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
    -

27
PracticeShow Stack for Each
  • 3 4 5 ( 7 - 3 )
  • ( 6 - ( 6 - 1) - (4 2 ) ) 5

28
Putting It Together
InfixToPostfix Program
Enter infix
Postfix file
EvaluatePostfix Program
Result
Postfix file
29
Example
  • 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

30
Application 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?
31
Problem 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.
32
What 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.
33
Examples
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.
Write a Comment
User Comments (0)
About PowerShow.com