stacks - simon fraser universitymitchell/cmpt-225/2020... · stack-• adt that stores a collection...
TRANSCRIPT
Stacks
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
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 . . .
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 .
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
"
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
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-④
staekImplemewtat.cn#- In both cases
,push 4 pop
are
constant time operations
( as long as the stack does not get largerthan the array ) .
End