CSc212AB Data Structures Lecture 17 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CSc212AB Data Structures Lecture 17

Description:

If get through w/o errors, then ok. Draw runs for egs. Complx for n = #chars? 9. 8/15/09 ... Might get op long before second operand. Have to remember op, ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: Zhiga6
Category:

less

Transcript and Presenter's Notes

Title: CSc212AB Data Structures Lecture 17


1
CSc212AB Data StructuresLecture 17
  • M. P. Johnson
  • mpjohnson_at_gmail.com
  • http//www-cs.ccny.cuny.edu/mjohnson/212/
  • City College of New York, Spring, 2005

2
Agenda
  • Stacks
  • Queues
  • Recursion
  • Hw4 up soon

3
New topic Stacks
  • Df ordered seq whose access is limited to
  • FILO LIFO
  • Simpler than list / list w/ limitation
  • But hugely important
  • 2nd after array
  • Pops up all the time
  • Idea each item stands on prev
  • Each item removed is the oldest

4
Stacks and the STL stack
  • Definition
  • A stack is a data structure of ordered entries
    such that entries can be inserted and removed at
    only one end (call the top)
  • LIFO
  • A stack is a Last-In/First-Out data structure.
    Entries are taken out of the stack in the reverse
    order of their insertion

5
Stack ops - LL
  • Push onto, pop off
  • Lunch room trays
  • Maybe also top look, dont remove
  • Can back with LL or array
  • LL

void stackpush(const dtype val)
list_head_insert(val)
dtype stackpop() dtype val
head-gtdata() list_head_remove()
6
Stack ops - array
  • Barring overflow, very fast
  • Have datacapacity

void stackpush(const dtype val)
datatop o
void stackpush(const dtype val) return
data--top
7
Stack apps
  • Implementation pretty simple
  • Just a restricted sequence
  • Lots of important apps
  • Simple one balancing symbols
  • Exprs in formal langs must be valid, i.e.,
    parseable
  • Html W3C
  • Alg expressions
  • C code
  • One simple rule s, () cant cross
  • Okay (), (), not ()(, )()(, ()
  • How to check?

8
Stack app 1 Balancing symbols alg
  • Start with empty stack
  • For each char
  • if opening sym, push
  • if closing sym, pop
  • error if empty
  • Error if not matching
  • If get through w/o errors, then ok
  • Draw runs for egs
  • Complx for n chars?

9
Stack app 2 parsing infix
  • 2345 ?
  • Normally interp as (23) (45) 26
  • Win calc Standard interps as ((23) 4) 5 50
  • Win calc sci doesnt
  • w/o parens, infix notation is ambig
  • Either way, Hard to parse
  • Might get op long before second operand
  • Have to remember op, retrieve when right
  • MR, MC?
  • Give eg

10
postfix
  • How to compute 23 45 with wincalcstd,
    conserving vars?
  • compute a1 2 3 2 3
  • compute a2 4 5 4 5
  • compute a1 a1a2 a1 a2
  • Can write as
  • 2 3 4 5
  • Postfix notation or reverse Polish
  • Polish is prefix 2 3
  • Jan Lukasiewicz

11
Parsing postfix
  • Parsing postfix much easier
  • No ambig
  • Simple alg
  • Walk through expr
  • See num ? push
  • See (bin) opp ? pop twice, apply op, push
  • When done, pop for result
  • Thats all!
  • Eg 652383/ ? 8

12
Stack v. Queue
  • Sequence of items
  • Queue FIFO enQ/deQ
  • Stack LIFO push/pop
  • List usually implemented as either
  • array
  • Linked list
  • Depending on freq of searchs v mods

13
Stack app 3 Infix ? Postfix
  • No one writes in postfix, so wheres it come
    from?
  • Must convert from postfix
  • With a stack
  • A bit more diff than parsing postfix, but not too
    bad

14
Infix ? Postfix rules
  • Read operand ? print operand
  • Read ( ? just push
  • Read ) ? pop, print all until first (
  • Parens not printed
  • Dont pop ( in any other case
  • Read ? pop stack until op w/ lwr prior
  • ( counts as lowest prior

15
Infix ? Postfix eg
  • A bc
  • A b c
  • A (bc)
  • Abc(def)g
  • ? A b c d e f g
Write a Comment
User Comments (0)
About PowerShow.com