stacks - simon fraser universitymitchell/cmpt-225/2020... · stack-• adt that stores a collection...

9
Stacks

Upload: others

Post on 08-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Stacks

Page 2: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Stack-

• ADT that stores a collection of objectsInsertions 4 removals follow a n .

Last- in- first- out pattern

< Basic Operations :

push : inserts an elementat the

'

top'

pop : removest returns the top

.

element

⇒⇒

'÷: 7¥: :*

emptystack

Page 3: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Stack=

-Convienient but optional operations :

isEmpty : check for emptiness n .

size : return # elements on stack

top : return top , but don't remove it

- Algorithmic applications :. parsing levaluat.com/transformation of expressionsIspeach recognition ; coding(decoding• shared network access control .

→ programming languages £ execution-Postscript , Forth , the call stack

• things we do daily with computers . . .

Page 4: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Parenthesising00⑨ a 0 0 @ 0 a 0 • a 0 a @ eg b @

41441*11×

Can be done by counting :

Chill 'D iciii 1) 111 1141 2 4 23 2 10 12 I 21 1 01 0 -7 I 0 -1

Checking Multiple Grouping Symbols:

(4) 1113 llllmnT TT pp

-

Counting is not enough .

Page 5: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Astack-basedalgorithmforcheckinggroupingsymbubss-ne.noempty stack

while there are symbols to readC ← next symbolif e is a left symbol

push con Selseif S is empty report errord * pop S

if c. d do not match reportend whileif 5 is not empty report error

report "ok

"

Page 6: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

5tack-partially-tikedArraytmplemewtationkariablesi.FI- array of stack

elements'

capacity - size of arraytop - index in array of top stack

element- -1 if stack is empty

Alot irrelevant A pacify- D

A I = !⇐capacity = {size of A)top = 2

push e If I

A-tT} = eggcapacity = {size of A)top =3

Page 7: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

Stack : Linked List Implementation-

t.tot.EDU 3 = ±fWhich end shout be the top ?• push = insert at top - top = front of list

= normalI linked list add - to - front

. pop= remove from front = top .

= normal linked list remove -from - front

i.FI#--GI5.FJ-oIaD-HJ=ab-

↳ pop→ D-④

Page 8: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

staekImplemewtat.cn#- In both cases

,push 4 pop

are

constant time operations

( as long as the stack does not get largerthan the array ) .

Page 9: Stacks - Simon Fraser Universitymitchell/cmpt-225/2020... · Stack-• ADT that stores a collection of objects Insertions 4 removals follow a n. Last-in-first-out pattern< Basic Operations

End