Stacks Stack - a collection of ADT objects that stores - - PowerPoint PPT Presentation
Stacks Stack - a collection of ADT objects that stores - - PowerPoint PPT Presentation
Stacks Stack - a collection of ADT objects that stores Insertions 4 removals follow a n . Last - in - first - out pattern < Basic Operations : ' ' top an element at the inserts push : removes t returns the top .
Stack
- ADT
that
stores
a collection of
- bjects
Insertions 4 removals follow
a n .
Last- in - first - out pattern
< Basic Operations:
push
:
inserts
an element at the
'
top
'
pop
:
removes t returns the top
. element
⇒
⇒
⇐
'
÷ :
7¥
: :*
empty
stack
Stack
=
- Convienient but optional operations :
isEmpty
:
check for emptiness
n .
size :
return
#
elements
- n stack
top
:
return top , but don't remove it
- Algorithmic applications :
. parsing levaluat.com/transformation of expressions
Ispeach recognition ;
coding(decoding
- shared
network access control
. → programming languages £ execution- Postscript , Forth ,
the call stack
- things
we do daily with computers
. . .
Parenthesising
00 ⑨ a
0 0
@ 0
a 0 •
a
a @ eg b @
41441*11
×
Can be done by counting
:
Chill 'D
iciii
1) 111
114
1
2
4 23
2 10
12 I 21
1
01
0 -7
I 0 -1
Checking Multiple Grouping Symbols
:
(4)
1113
llllmn
T
TT
pp
- Counting is
not
enough
.Astack-basedalgorithmforcheckinggroupingsymbubss-ne.no
empty
stack
while
there
are
symbols
to read
C ←
next
symbol
if
e
is
a
left
symbol
push
con
S
else
if
S
is empty
report
error d * pop
S
if
- c. d
do
not match report
end
while
if
5
is
not
empty
report
error
report
" ok
"
5tack-partially-tikedArraytmplemewtationkariablesi.FI
- array of stack elements
'
capacity
- size of array
top
- 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}
=
egg
capacity
= {size of A)
top =3
Stack : Linked List Implementation
- t.tot.EDU
3
= ±f
Which 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=a
b-
↳ pop → D-④
staekImplemewtat.cn#
- In
both
cases ,
push
4
pop
are
constant time
- perations
( as long
as the
stack does not get
larger
than
the
array ) .