Stacks - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Stacks

Description:

DIY by hand 2710 - XXXX2, show the stack contents on each step. 6/22/09. ECE304 Meng-Lai Yin ... Add an indicator for the top pointer of an array (still, this ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 14
Provided by: csupo
Category:
Tags: diy | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks Queues(Ch. 4 5 in text)
2
Base-Conversion Algorithm
  • Convert a base-10 number to base-2
  • Algorithm input, output, steps
  • Use Stack data structure
  • Push the remainders into the stack
  • Pop the data out
  • DIY by hand 2710 - XXXX2, show the stack
    contents on each step

3
Implementing a Stack class
  • Data members data type
  • How do you store the items in a stack?
  • Use array (any drawback?)
  • Add an indicator for the top pointer of an array
    (still, this implementation has the capability
    issue)
  • Linked list
  • Member functions
  • Empty
  • Push
  • Pop

4
Exercise 4.2
  • Problem 10 the railroad switching network
  • When push, the car on the right (before the
    intersection) will go into the siding. When
    move, the car goes directly to the left. When
    pop, the car on the top of the siding will go to
    the left.
  • For n3, there are totally 6321 permutations.
    What operations will be needed for each one? Is
    there a permutation not possible (312)
  • Homework 10(b)

5
Reverse Polish Notation (RPN)
  • Infix notation 2(34)
  • Postfix 234
  • Note no parenthesis is needed
  • Application Compiler. Postfix expressions are
    easier to evaluate mechanically

6
Evaluation RPN
  • Scanned the expression from left to right until
    an operator is found
  • The last two operands are combined using this
    operator just found
  • Replace the sub-expression with the value
    calculated, obtain a reduced RPN
  • Repeat the above process until the result is
    yield and no more operator exists

7
RPN Evaluation examples
  • 15841--
  • 2495-

8
Converting Infix Expressions to RPN
  • Using expression trees
  • ab
  • a(bc)
  • (ab)(c/(d-e))
  • Traverse the tree in a Left-Right-Parent order
  • ab
  • ?
  • ?

9
Converting (from infix to postfix) algorithm
  • Fully parenthesize the expression
  • Replace each right parenthesis by the
    corresponding operator
  • Erase all left parenthesis
  • Example ((ab)c)
  • - ((ab) c
  • - ((a b c
  • - abc

10
Exercises 4.3
  • 1-11
  • 12-14
  • 15-22
  • 28-35
  • 46-52

11
Basics of Queues
front
back
  • FIFO or FIFS
  • Basic operations in text
  • Empty
  • AddQ (add an element at the back of the queue)
  • Front (retrieve the element at the front of the
    queue)
  • RemoveQ (remove the element at the front of the
    queue)

12
Implementation of Queues
  • Array-based
  • The shift back issue (page 221) cause
    inefficiency
  • Can be avoided by thinking of the array as
    circular, with first element following the last
    (page 222)

13
Exercises 5.2
  • 1-4
  • 14
Write a Comment
User Comments (0)
About PowerShow.com