Stacks and Applications - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Stacks and Applications

Description:

Postfix or. reverse Polish notation. Write operator after the two numbers being combined: 5 8 ... Evaluating postfix notation. pseudocode: while there is still ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 17
Provided by: shar305
Category:

less

Transcript and Presenter's Notes

Title: Stacks and Applications


1
Stacks and Applications
2
Stack
  • A stack is a data structure of entries in order.
  • Entries can be inserted and removed only at one
    end - the top.
  • Insert an entry push operation.
  • Remove an entry pop operation.
  • A stack is a Last in/First out data structure
    -LIFO

3
Most important
  • A stack can only
  • be accessed
  • at the top.

4
Another stack
5
Application Matching parentheses
  • pseudocode
  • while the expression still has entries
  • read a character c
  • if c is (, put ( on the stack.
  • if c is ), pop symbol from stack. If the stack
    is empty, parentheses are unbalanced.
  • if c is anything else, do nothing.
  • if stack is not empty, then parentheses are
  • unbalanced.
  • else parentheses are balanced.

6
Apply the algorithm to
  • (37(75)(9-10))
  • 6((5-4)7(513)
  • (7-6(59(4-2))))
  • Show the stacks
  • Can you improve the algorithm so it tells whether
    too many ( or too many )?

7
Application Evaluating parenthesized expressions
  • An expression is completely parenthesized,
    contains numbers, blanks, operations.
  • Always evaluate the left-most subexpression that
    has no inner parentheses, and replace it with its
    value.
  • ((6(12/4))(2(7-2)))
  • Right parentheses tell us when to evaluate.
  • Use stacks to keep track of numbers, operators.

8
Application Evaluating parenthesized expressions
  • pseudocode
  • while there is still input
  • read an item
  • if it is a number, put it on the number stack
  • if it is an operator, put it on the operator
    stack
  • if it is a left parenthesis or blank, do
    nothing.
  • if it is a right parenthesis, take the top two
  • numbers from the number stack, and the top
  • operation from the operator stack, combine the
  • numbers with the operator, and put the result
    onto
  • the number stack.
  • The final result is the number on the number
    stack.

9
Evaluate this expression
  • Follow the pseudocode.
  • Write out the stacks.
  • (((129)/3)6)((6-4)/6))

10
STL stack interface p.350
  • Some members of the stackltItemgt Class from
    ltstackgt
  • // TYPEDEFS
  • // value_type the data type of the items in the
  • //stack from the Item parameter
  • // size_type the data type for a variable that
  • //keeps track of how many items in a stack
  • // CONSTRUCTOR Default constructor Creates an
    empty
  • //stack
  • // VOID FUNCTIONS TO INSERT AND REMOVE ITEMS
  • // pop( ) pop is a void function that
    pops(removes)
  • //the top item of the stack
  • // push(const Item entry) Pushes the item onto
    the
  • //top of the stack

11
STL stack interface
  • Some members of the stackltItemgt Class from
    ltstackgt
  • // FUNCTIONS TO EXAMINE THE STACK AND ITS ITEMS
  • // empty( ) const Returns true if the stack is
    empty
  • //(otherwise returns false)
  • // size( ) const Returns the number of items in
    the
  • //stack
  • // top( ) Returns a reference to the top item
    on
  • //the stack (without removing it)
  • // VALUE SEMANTICS
  • // Assignments and the copy constructor may be
    used
  • //with stackltItemgt objects.

12
STL Programmers Guide
  • http//www.sgi.com/tech/stl/stack.html

13
Postfix or reverse Polish notation
  • Write operator after the two numbers being
    combined 5 8
  • Can write expressions without parentheses and
    without hierarchy of operations 5 8 3 6
  • Invented by Jan Lukasiewicz, Polish logician

14
Used on early HP calculators
  • http//www.hpmuseum.org/rpn.htm
  • Easy to program uses one stack
  • Makes user do the work of converting expression
    to reverse Polish

15
Application Evaluating postfix notation
  • pseudocode
  • while there is still input
  • read an item
  • if it is a number, put it on the stack
  • if it is an operator, pop two numbers from
    the stack, combine them with the
    operator, and push the result back onto
    stack.
  • The final result is the number on the stack.

16
Evaluate
  • Follow the pseudocode show the stack.
  • 5 8 3 6
  • 5 7 8 12 /
Write a Comment
User Comments (0)
About PowerShow.com