S t a c k s a n d q u e u e s S t a c k s A s - - PowerPoint PPT Presentation

s t a c k s a n d q u e u e s s t a c k s
SMART_READER_LITE
LIVE PREVIEW

S t a c k s a n d q u e u e s S t a c k s A s - - PowerPoint PPT Presentation

S t a c k s a n d q u e u e s S t a c k s A s t a c k s t o r e s a s e q u e n c e o f v a l u e s Ma i n o p e r a t i o n s : p u s h ( x ) a d d v a l u e x


slide-1
SLIDE 1

S t a c k s a n d q u e u e s

slide-2
SLIDE 2

S t a c k s

A s t a c k s t

  • r

e s a s e q u e n c e

  • f

v a l u e s Ma i n

  • p

e r a t i

  • n

s :

  • p

u s h ( x ) – a d d v a l u e x t

  • t

h e s t a c k

  • p
  • p

( ) – r e m

  • v

e t h e m

  • s

t

  • r

e c e n t l y

  • p

u s h e d v a l u e f r

  • m

t h e s t a c k

L I F O : l a s t i n fj r s t

  • u

t

  • V

a l u e r e m

  • v

e d b y p

  • p

i s a l w a y s t h e

  • n

e t h a t w a s p u s h e d m

  • s

t r e c e n t l y

slide-3
SLIDE 3

S t a c k s

A n a l

  • g

y f

  • r

L I F O : s t a c k

  • f

p l a t e s

  • C

a n

  • n

l y a d d

  • r

r e m

  • v

e p l a t e s a t t h e t

  • p

!

  • Y
  • u

a l w a y s t a k e

  • fg

t h e m

  • s

t r e c e n t p l a t e

slide-4
SLIDE 4

S t a c k s

Mo r e s t a c k

  • p

e r a t i

  • n

s :

  • i

s s t a c k e m p t y ? – i s t h e r e a n y t h i n g

  • n

t h e s t a c k ?

  • t
  • p

( ) – r e t u r n m

  • s

t

  • r

e c e n t l y

  • p

u s h e d ( “ t

  • p

” ) v a l u e w i t h

  • u

t r e m

  • v

i n g i t

slide-5
SLIDE 5

E x a m p l e : b a l a n c e d b r a c k e t s

G i v e n a s t r i n g :

“ h e l l

  • (

h e l l

  • i

s a g r e e t n g [ s i c ] { “ s i c ” i s u s e d w h e n q u

  • t

i n g a t e x t t h a t c

  • n

t a i n s a t y p

  • (
  • r

a r c h a i c [ a n d n

  • w

a d a y s w r

  • n

g ] s p e l l i n g ) t

  • s

h

  • w

t h a t t h e m i s t a k e w a s i n t h e

  • r

i g i n a l t e x t ( a n d n

  • t

i n t r

  • d

u c e d w h i l e c

  • p

y i n g t h e q u

  • t

e ) } ) ”

C h e c k t h a t a l l b r a c k e t s m a t c h :

  • E

v e r y

  • p

e n i n g b r a c k e t h a s a c l

  • s

i n g b r a c k e t

  • E

v e r y c l

  • s

i n g b r a c k e t h a s a n

  • p

e n i n g b r a c k e t

  • N

e s t e d b r a c k e t s m a t c h u p : n

( [ ) ] ” !

slide-6
SLIDE 6

A l g

  • r

i t h m

Ma i n t a i n a s t a c k

  • f
  • p

e n e d b r a c k e t s

  • I

n i t i a l l y s t a c k i s e m p t y

  • G
  • t

h r

  • u

g h s t r i n g

  • n

e c h a r a c t e r a t a t i m e

  • I

f w e s e e a n

  • p

e n i n g b r a c k e t , p u s h i t

  • I

f w e s e e a c l

  • s

i n g b r a c k e t , p

  • p

f r

  • m

t h e s t a c k a n d c h e c k t h a t i t m a t c h e s

– e

. g . , i f w e s e e a “ ) ” , c h e c k t h a t t h e p

  • p

p e d v a l u e i s a “ ( “

  • Wh

e n w e g e t t

  • t

h e e n d

  • f

t h e s t r i n g , c h e c k t h a t t h e s t a c k i s e m p t y

slide-7
SLIDE 7

A l g

  • r

i t h m

Ma i n t a i n a s t a c k

  • f
  • p

e n e d b r a c k e t s

  • I

n i t i a l l y s t a c k i s e m p t y

  • G
  • t

h r

  • u

g h s t r i n g

  • n

e c h a r a c t e r a t a t i m e

  • I

f w e s e e a n

  • p

e n i n g b r a c k e t , p u s h i t

  • I

f w e s e e a c l

  • s

i n g b r a c k e t , p

  • p

f r

  • m

t h e s t a c k a n d c h e c k t h a t i t ma t c h e s

– e

. g . , i f w e s e e a “ ) ” , c h e c k t h a t t h e p

  • p

p e d v a l u e i s a “ ( “

  • Wh

e n w e g e t t

  • t

h e e n d

  • f

t h e s t r i n g , c h e c k t h a t t h e s t a c k i s e mp t y

C h e c k y

  • u

r u n d e r s t a n d i n g : Wh a t h a s g

  • n

e w r

  • n

g i f e a c h

  • f

t h e s t e p s w r i t t e n i n b

  • l

d f a i l s ? ( s t a c k c a n b e e m p t y )

slide-8
SLIDE 8

M

  • r

e u s e s

  • f

s t a c k s

Tie c a l l s t a c k , w h i c h i s u s e d b y t h e p r

  • c

e s s

  • r

t

  • h

a n d l e f u n c t i

  • n

c a l l s

  • Wh

e n y

  • u

c a l l a f u n c t i

  • n

, t h e p r

  • c

e s s

  • r

r e c

  • r

d s w h a t i t w a s d

  • i

n g b y p u s h i n g a r e c

  • r

d

  • n

t

  • t

h e c a l l s t a c k

  • Wh

e n a f u n c t i

  • n

r e t u r n s , t h e p r

  • c

e s s

  • r

p

  • p

s a r e c

  • r

d

  • fg

t h e c a l l s t a c k t

  • s

e e w h a t i t s h

  • u

l d c a r r y

  • n

d

  • i

n g

P a r s i n g i n c

  • m

p i l e r s L

  • t

s

  • f

u s e s i n a l g

  • r

i t h m s !

slide-9
SLIDE 9

S t a c k s i n H a s k e l l a r e j u s t l i s t s

type Stack a = [a] push :: a Stack a Stack a → → push x xs = x:xs pop :: Stack a Stack a → pop (x:xs) = xs top :: Stack a a → top (x:xs) = x empty :: Stack a Bool → empty [] = True empty (x:xs) = False

Y

  • u

d

  • n

' t n e e d a s e p a r a t e s t a c k t y p e i f y

  • u

h a v e H a s k e l l

  • s

t y l e l i s t s

slide-10
SLIDE 10

I m p l e m e n t i n g s t a c k s i n J a v a

I d e a : u s e a d y n a m i c a r r a y !

  • P

u s h : a d d a n e w e l e m e n t t

  • t

h e e n d

  • f

t h e a r r a y

  • P
  • p

: r e m

  • v

e e l e m e n t f r

  • m

t h e e n d

  • f

t h e a r r a y

C

  • m

p l e x i t y : a l l

  • p

e r a t i

  • n

s h a v e a m

  • r

t i s e d O ( 1 ) c

  • m

p l e x i t y

  • Me

a n s : n

  • p

e r a t i

  • n

s t a k e O ( n ) t i m e

  • W

e d

  • n

' t s t u d y a m

  • r

t i s e d c

  • m

p l e x i t y i n t h i s c

  • u

r s e

  • A

l t h

  • u

g h a s i n g l e

  • p

e r a t i

  • n

m a y t a k e O ( n ) t i m e , a n “ e x p e n s i v e ”

  • p

e r a t i

  • n

i s a l w a y s b a l a n c e d

  • u

t b y a l

  • t
  • f

e a r l i e r “ c h e a p ”

  • p

e r a t i

  • n

s

slide-11
SLIDE 11

A b s t r a c t d a t a t y p e s

Y

  • u

s h

  • u

l d d i s t i n g u i s h b e t w e e n :

  • t

h e a b s t r a c t d a t a t y p e ( A D T ) ( a s t a c k )

  • i

t s i m p l e m e n t a t i

  • n

( e . g . a d y n a m i c a r r a y )

Wh y ?

  • Wh

e n y

  • u

u s e a d a t a s t r u c t u r e y

  • u

d

  • n

' t c a r e h

  • w

i t ' s i m p l e m e n t e d

  • Mo

s t A D T s h a v e m a n y p

  • s

s i b l e i m p l e m e n t a t i

  • n

s

slide-12
SLIDE 12

Q u e u e s

A q u e u e a l s

  • s

t

  • r

e s a s e q u e n c e

  • f

v a l u e s Ma i n

  • p

e r a t i

  • n

s :

  • e

n q u e u e ( x ) – a d d v a l u e x t

  • t

h e q u e u e

  • d

e q u e u e ( ) – r e m

  • v

e e a r l i e s t

  • a

d d e d v a l u e

F I F O : fj r s t i n fj r s t

  • u

t

  • V

a l u e d e q u e u e d i s a l w a y s t h e

  • l

d e s t

  • n

e t h a t ' s s t i l l i n t h e q u e u e

Mu c h l i k e a s t a c k – b u t F I F O , n

  • t

L I F O

slide-13
SLIDE 13

Q u e u e s

L i k e a q u e u e i n r e a l l i f e !

  • Tie

fj r s t t

  • e

n t e r t h e q u e u e i s t h e fj r s t t

  • l

e a v e

slide-14
SLIDE 14

U s e s

  • f

q u e u e s

C

  • n

t r

  • l

l i n g a c c e s s t

  • s

h a r e d r e s

  • u

r c e s i n a n

  • p

e r a t i n g s y s t e m , e . g . a p r i n t e r q u e u e A q u e u e

  • f

r e q u e s t s i n a w e b s e r v e r A l s

  • a

p p e a r s i n l

  • t

s

  • f

a l g

  • r

i t h m s

  • (

S t a c k s a n d q u e u e s b

  • t

h a p p e a r w h e n a n a l g

  • r

i t h m h a s t

  • r

e m e m b e r a l i s t

  • f

t h i n g s t

  • d
  • )
slide-15
SLIDE 15

I m p l e m e n t i n g q u e u e s i n J a v a

Wh a t ' s w r

  • n

g w i t h t h i s i d e a ?

  • I

m p l e m e n t t h e q u e u e a s a d y n a m i c a r r a y

  • e

n q u e u e ( x ) : a d d x t

  • t

h e e n d

  • f

t h e d y n a m i c a r r a y

  • d

e q u e u e ( ) : r e m

  • v

e a n d r e t u r n fj r s t e l e m e n t

  • f

a r r a y

T

  • d

e q u e u e , w e ' d h a v e t

  • c
  • p

y t h e e n t i r e r e s t

  • f

t h e a r r a y d

  • w

n

  • n

e p l a c e . . . t a k e s O ( n ) t i m e

slide-16
SLIDE 16

D y n a m i c a r r a y s a r e n

  • g
  • d

A q u e u e c

  • n

t a i n i n g A , B , C : D e q u e u e r e m

  • v

e s A : Mo v i n g t h e r e s t

  • f

t h e q u e u e i n t

  • p

l a c e t a k e s O ( n ) t i m e !

A B C B C

slide-17
SLIDE 17

B

  • u

n d e d q u e u e s

L e t ' s s

  • l

v e a s i m p l e r p r

  • b

l e m fj r s t : b

  • u

n d e d q u e u e s A b

  • u

n d e d q u e u e i s a q u e u e w i t h a fj x e d c a p a c i t y , e . g . 5

  • Tie

q u e u e c a n ' t c

  • n

t a i n m

  • r

e t h a n 5 e l e m e n t s a t a t i m e

  • Y
  • u

t y p i c a l l y c h

  • s

e t h e c a p a c i t y w h e n y

  • u

c r e a t e t h e q u e u e

slide-18
SLIDE 18

B

  • u

n d e d q u e u e s

A n a r r a y , p l u s t w

  • i

n d i c e s b a c k a n d f r

  • n

t b a c k : w h e r e w e e n q u e u e t h e n e x t e l e m e n t f r

  • n

t : w h e r e w e d e q u e u e t h e n e x t e l e m e n t

A B C b a c k f r

  • n

t Q u e u e c

  • n

t a i n s A , B , C

slide-19
SLIDE 19

B

  • u

n d e d q u e u e s

A f t e r e n q u e u e i n g D a r r a y [ b a c k ] = D ; b a c k = b a c k + 1

A B C D b a c k f r

  • n

t Q u e u e c

  • n

t a i n s A , B , C , D

slide-20
SLIDE 20

B

  • u

n d e d q u e u e s

A f t e r d e q u e u e i n g ( t

  • g

e t A ) r e s u l t = a r r a y [ f r

  • n

t ] ; f r

  • n

t = f r

  • n

t + 1

B C D b a c k f r

  • n

t Q u e u e c

  • n

t a i n s B , C , D

slide-21
SLIDE 21

T h i n k i n g f

  • r

m a l l y a b

  • u

t q u e u e s

Wh a t i s t h e c

  • n

t e n t s

  • f
  • n

e

  • f
  • u

r a r r a y

  • q

u e u e s ?

  • E

v e r y t h i n g f r

  • m

i n d e x f r

  • n

t t

  • i

n d e x b a c k

  • 1

I f w e s p e c i f y t h e m e a n i n g

  • f

t h e a r r a y l i k e t h i s , t h e r e i s

  • n

l y

  • n

e s e n s i b l e w a y t

  • i

m p l e m e n t e n q u e u e a n d d e q u e u e !

  • B

e f

  • r

e d e q u e u e : c

  • n

t e n t s i s a r r a y [ f r

  • n

t ] , a r r a y [ f r

  • n

t + 1 ] , … , a r r a y [ b a c k

  • 1

]

  • A

f t e r d e q u e u e : a r r a y [ f r

  • n

t ] s h

  • u

l d b e g

  • n

e , c

  • n

t e n t s i s a r r a y [ f r

  • n

t + 1 ] , … , a r r a y [ b a c k

  • 1

]

  • O

n l y g

  • d

w a y t

  • d
  • t

h i s i s f r

  • n

t = f r

  • n

t + 1 !

D a t a s t r u c t u r e d e s i g n h i n t : d

  • n

' t j u s t t h i n k w h a t e v e r y t h i n g s h

  • u

l d d

  • !

Wo r k

  • u

t t h e m e a n i n g

  • f

t h e d a t a s t r u c t u r e t

  • .
slide-22
SLIDE 22

B

  • u

n d e d q u e u e s

A f t e r e n q u e u e i n g E a n d d e q u e u e i n g Wh a t ' s t h e p r

  • b

l e m h e r e ?

C D E b a c k f r

  • n

t

slide-23
SLIDE 23

Q u e u e s a s c i r c u l a r b u fg e r s

P r

  • b

l e m : w h e n b a c k r e a c h e s t h e e n d

  • f

t h e a r r a y , w e c a n ' t e n q u e u e a n y t h i n g e l s e I d e a : c i r c u l a r b u fg e r

  • Wh

e n b a c k r e a c h e s t h e e n d

  • f

t h e a r r a y , p u t t h e n e x t e l e m e n t a t i n d e x – a n d s e t b a c k t

  • N

e x t a f t e r t h a t g

  • e

s a t i n d e x 1

  • f

r

  • n

t w r a p s a r

  • u

n d i n t h e s a m e w a y

U s e a l l t h e e x t r a s p a c e t h a t ' s l e f t i n t h e b e g i n n i n g

  • f

t h e a r r a y a f t e r w e d e q u e u e !

slide-24
SLIDE 24

B

  • u

n d e d q u e u e s

T r y a g a i n – a f t e r e n q u e u e i n g E b a c k w r a p s a r

  • u

n d t

  • i

n d e x

C D E b a c k f r

  • n

t

slide-25
SLIDE 25

B

  • u

n d e d q u e u e s

N

  • w

a f t e r e n q u e u e i n g F Me a n i n g : q u e u e c

  • n

t a i n s e v e r y t h i n g f r

  • m

f r

  • n

t t

  • b

a c k

  • 1

s t i l l . B u t w r a p p i n g a r

  • u

n d i f b a c k < f r

  • n

t ! E x e r c i s e : p h r a s e t h i s p r e c i s e l y .

F C D E b a c k f r

  • n

t Q u e u e c

  • n

t a i n s C , D , E , F

slide-26
SLIDE 26

B

  • u

n d e d q u e u e s

A f t e r d e q u e u e i n g t w i c e

F E b a c k f r

  • n

t Q u e u e c

  • n

t a i n s E , F

slide-27
SLIDE 27

B

  • u

n d e d q u e u e s

A f t e r d e q u e u e i n g a g a i n f r

  • n

t w r a p s a r

  • u

n d t

  • !

F b a c k f r

  • n

t Q u e u e c

  • n

t a i n s F

slide-28
SLIDE 28

C i r c u l a r b u fg e r s

B a s i c i d e a : a n a r r a y , p l u s t w

  • i

n d i c e s f

  • r

t h e f r

  • n

t a n d b a c k

  • f

t h e q u e u e

  • Tie

s e i n d i c e s w r a p a r

  • u

n d w h e n r e a c h i n g t h e e n d

  • f

t h e a r r a y , w h i c h i s w h a t m a k e s i t w

  • r

k

E x e r c i s e : w h a t s e q u e n c e

  • f

e l e m e n t s d

  • e

s a c i r c u l a r b u fg e r r e p r e s e n t ? Tie b e s t b

  • u

n d e d q u e u e i m p l e m e n t a t i

  • n

!

slide-29
SLIDE 29

B

  • u

n d e d q u e u e s

C i r c u l a r b u fg e r s m a k e a fj n e b

  • u

n d e d q u e u e T

  • m

a k e a n u n b

  • u

n d e d q u e u e , l e t ' s b e i n s p i r e d b y d y n a m i c a r r a y s

  • D

y n a m i c a r r a y s : fj x e d

  • s

i z e a r r a y , d

  • u

b l e t h e s i z e w h e n i t g e t s f u l l

  • U

n b

  • u

n d e d q u e u e s : b

  • u

n d e d q u e u e , d

  • u

b l e t h e c a p a c i t y w h e n i t g e t s f u l l

Wh e n e v e r t h e q u e u e g e t s f u l l , a l l

  • c

a t e a n e w q u e u e

  • f

d

  • u

b l e t h e c a p a c i t y , a n d c

  • p

y t h e

  • l

d q u e u e t

  • t

h e n e w q u e u e

slide-30
SLIDE 30

R e a l l

  • c

a t i

  • n

, h

  • w

n

  • t

t

  • d
  • i

t

Wh a t ' s w r

  • n

g w i t h r e s i z i n g l i k e t h i s ?

F G C D E b a c k f r

  • n

t F G C D E b a c k f r

  • n

t

slide-31
SLIDE 31

R e a l l

  • c

a t i

  • n

, h

  • w

n

  • t

t

  • d
  • i

t

Wh a t ' s w r

  • n

g w i t h r e s i z i n g l i k e t h i s ?

F G C D E b a c k f r

  • n

t F G C D E b a c k f r

  • n

t Q u e u e c

  • n

t a i n s C , D , E , fj v e b l a n k s p a c e s , F , G !

slide-32
SLIDE 32

R e a l l

  • c

a t i

  • n

, h

  • w

n

  • t

t

  • d
  • i

t

I n s t e a d , r e p e a t e d l y d e q u e u e f r

  • m

t h e

  • l

d q u e u e a n d e n q u e u e i n t

  • t

h e n e w q u e u e :

F G C D E b a c k f r

  • n

t C D E F G b a c k f r

  • n

t

slide-33
SLIDE 33

S u m m a r y : q u e u e s a s a r r a y s

Ma i n t a i n f r

  • n

t a n d b a c k i n d e x e s

  • E

n q u e u e e l e m e n t s a t b a c k , r e m

  • v

e f r

  • m

f r

  • n

t

C i r c u l a r a r r a y

  • f

r

  • n

t a n d b a c k w r a p a r

  • u

n d w h e n t h e y r e a c h t h e e n d

I d e a f r

  • m

d y n a m i c a r r a y s

  • Wh

e n t h e q u e u e g e t s f u l l , a l l

  • c

a t e a n e w

  • n

e

  • f

t w i c e t h e s i z e

  • D
  • n

' t j u s t r e s i z e t h e a r r a y – s a f e r t

  • u

s e t h e q u e u e

  • p

e r a t i

  • n

s t

  • c
  • p

y f r

  • m

t h e

  • l

d q u e u e t

  • t

h e n e w q u e u e

I m p

  • r

t a n t i m p l e m e n t a t i

  • n

n

  • t

e !

  • T
  • t

e l l w h e n a r r a y i s f u l l , n e e d a n e x t r a v a r i a b l e t

  • h
  • l

d t h e c u r r e n t s i z e

  • f

t h e q u e u e ( e x e r c i s e : w h y ? )

slide-34
SLIDE 34

Q u e u e s i n H a s k e l l

type Queue a = ??? enqueue :: a Queue a Queue a → → dequeue :: Queue a (a, Queue a) → empty :: Queue a Bool → [ b e t t e r A P I :

dequeue :: Queue a Maybe (a, Queue a) →

]

slide-35
SLIDE 35

O n e p

  • s

s i b i l i t y : u s i n g a l i s t

type Queue a = [a] enqueue :: a Queue a Queue a → → enqueue x xs = xs ++ [x] dequeue :: Queue a (a, Queue a) → dequeue (x:xs) = (x, xs) empty :: Queue a Bool → empty [] = True empty (x:xs) = False

Wh y n

  • t

d

  • i

t l i k e t h i s ?

slide-36
SLIDE 36

A c u n n i n g p l a n

I m p l e m e n t a q u e u e u s i n g t w

  • l

i s t s , t h e “ f r

  • n

t p a r t ” a n d t h e “ b a c k p a r t ” f r

  • n

t p a r t b a c k p a r t E n q u e u e i n t

  • t

h e b a c k p a r t , d e q u e u e f r

  • m

t h e f r

  • n

t p a r t – a n d m

  • v

e i t e m s f r

  • m

t h e b a c k t

  • t

h e f r

  • n

t w h e n n e e d e d

A B C E D Q u e u e c

  • n

t a i n s A B C D E

slide-37
SLIDE 37

A c u n n i n g p l a n

E n q u e u i n g F : f r

  • n

t p a r t b a c k p a r t O n l y n e e d t

  • u

s e c

  • n

s – c

  • n

s t a n t t i m e

A B C F D Q u e u e c

  • n

t a i n s A B C D E F E

slide-38
SLIDE 38

A c u n n i n g p l a n

D e q u e u e i n g A , B f r

  • n

t p a r t b a c k p a r t O n l y n e e d t

  • l
  • k

a t f r

  • n

t

  • f

l i s t – c

  • n

s t a n t t i m e

C F D Q u e u e c

  • n

t a i n s C D E F E

slide-39
SLIDE 39

A c u n n i n g p l a n

D e q u e u e i n g C f r

  • n

t p a r t b a c k p a r t Wh a t i f w e w a n t t

  • d

e q u e u e a g a i n ?

F D Q u e u e c

  • n

t a i n s D E F E

slide-40
SLIDE 40

A c u n n i n g p l a n

Wh e n t h e f r

  • n

t p a r t i s e m p t y , r e v e r s e t h e b a c k p a r t a n d m

  • v

e i t t h e r e ! f r

  • n

t p a r t b a c k p a r t N

  • w

w e c a n d e q u e u e a g a i n !

D F Q u e u e c

  • n

t a i n s D E F E

slide-41
SLIDE 41

Q u e u e s i n H a s k e l l

A q u e u e i s a p a i r

  • f

l i s t s

  • data Queue a = Queue { front :: [a], back :: [a] }
  • T
  • e

n q u e u e a n e l e m e n t , a d d i t t

  • back
  • T
  • d

e q u e u e , r e m

  • v

e a n e l e m e n t f r

  • m

front

  • I

f front i s e m p t y , r e p l a c e i t w i t h reverse back

Tie q u e u e Queue front back r e p r e s e n t s t h e s e q u e n c e front ++ reverse back

  • F
  • r

e x a m p l e , Queue [1,2,3] [6,5,4] r e p r e s e n t s t h e q u e u e 1 2 3 4 5 6

  • B

y w r i t i n g t h i s d

  • w

n , w e s e e w h y w e n e e d t

  • r

e v e r s e w h e n m

  • v

i n g back t

  • front!
slide-42
SLIDE 42

I s t h i s e ffj c i e n t ?

I s n ' t t h i s s l

  • w

? reverse t a k e s O ( n ) t i m e N

  • :

w e g e t a m

  • r

t i s e d O ( 1 ) c

  • m

p l e x i t y I f w e e n q u e u e a n d d e q u e u e n i t e m s . . .

  • W

e s p e n d s

  • m

e t i m e r e v e r s i n g s t u fg

  • B

u t

  • n

l y t h e s t u fg w e e n q u e u e g e t s r e v e r s e d , a n d e a c h i t e m i s

  • n

l y a d d e d t

  • back
  • n

c e , s

  • t

h e l i s t s w e r e v e r s e c

  • n

t a i n n i t e m s i n t

  • t

a l

  • S
  • t

h e r e v e r s i n g t a k e s O ( n ) t i m e f

  • r

n i t e m s

  • → O

( 1 ) t i m e a v e r a g e p e r i t e m e n q u e u e d

slide-43
SLIDE 43

D

  • u

b l e

  • e

n d e d q u e u e s

S

  • f

a r w e h a v e s e e n :

  • Q

u e u e s – a d d e l e m e n t s t

  • n

e e n d a n d r e m

  • v

e t h e m f r

  • m

t h e

  • t

h e r e n d

  • S

t a c k s – a d d a n d r e m

  • v

e e l e m e n t s f r

  • m

t h e s a m e e n d

I n a d e q u e , y

  • u

c a n a d d a n d r e m

  • v

e e l e m e n t s f r

  • m

b

  • t

h e n d s

  • a

d d t

  • f

r

  • n

t , a d d t

  • r

e a r

  • r

e m

  • v

e f r

  • m

f r

  • n

t , r e m

  • v

e f r

  • m

r e a r

G

  • d

n e w s – c i r c u l a r a r r a y s s u p p

  • r

t t h i s e a s i l y

  • F
  • r

t h e f u n c t i

  • n

a l v e r s i

  • n

, h a v e t

  • b

e a b i t c a r e f u l t

  • g

e t t h e r i g h t c

  • m

p l e x i t y – s e e e x e r c i s e

slide-44
SLIDE 44

I n p r a c t i c e

Y

  • u

r f a v

  • u

r i t e p r

  • g

r a m m i n g l a n g u a g e s h

  • u

l d h a v e a l i b r a r y m

  • d

u l e f

  • r

s t a c k s , q u e u e s a n d d e q u e s

  • J

a v a : u s e java.util.Deque<E> – p r

  • v

i d e s addFirst/ Last, removeFirst/ Last m e t h

  • d

s

  • N
  • t

e : J a v a a l s

  • p

r

  • v

i d e s a Stack c l a s s , b u t t h i s i s d e p r e c a t e d – d

  • n

' t u s e i t

  • H

a s k e l l : i n s t e a d

  • f

a s t a c k , j u s t u s e a l i s t

  • F
  • r

q u e u e s a n d d e q u e s , u s e Data.Sequence – a g e n e r a l

  • p

u r p

  • s

e s e q u e n c e d a t a t y p e

slide-45
SLIDE 45

S t a c k s , q u e u e s , d e q u e s – s u m m a r y

A l l t h r e e e x t r e m e l y c

  • m

m

  • n
  • S

t a c k s : L I F O , q u e u e s : F I F O , d e q u e s : g e n e r a l i s e b

  • t

h

  • O

f t e n u s e d t

  • m

a i n t a i n a s e t

  • f

t a s k s t

  • d
  • l

a t e r

  • I

m p e r a t i v e l a n g u a g e : s t a c k s a r e d y n a m i c a r r a y , q u e u e s a r e c i r c u l a r b u fg e r s , O ( 1 ) a m

  • r

t i s e d c

  • m

p l e x i t y

  • F

u n c t i

  • n

a l l a n g u a g e : s t a c k s a r e l i s t s , d e q u e s c a n b e i m p l e m e n t e d a s a p a i r

  • f

l i s t s w i t h O ( 1 ) a m

  • r

t i s e d c

  • m

p l e x i t y

D a t a s t r u c t u r e d e s i g n h i n t : a l w a y s t h i n k a b

  • u

t w h a t t h e r e p r e s e n t a t i

  • n
  • f

a d a t a s t r u c t u r e m e a n s !

  • e

. g . “ w h a t q u e u e d

  • e

s t h i s c i r c u l a r b u fg e r r e p r e s e n t ? ”

  • Tii

s i s t h e m a i n d e s i g n d e c i s i

  • n

y

  • u

h a v e t

  • m

a k e – i t d r i v e s e v e r y t h i n g e l s e

  • Tii

s l e t s y

  • u

d e s i g n n e w d a t a s t r u c t u r e s s y s t e m a t i c a l l y

  • A

n d a l s

  • u

n d e r s t a n d e x i s t i n g

  • n

e s