s t a c k s a n d q u e u e s w e i s s c h a p t e r 3 s
play

S t a c k s a n d q u e u e s ( W e i s s c h - PowerPoint PPT Presentation

S t a c k s a n d q u e u e s ( W e i s s c h a p t e r 3 ) 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 M a i n o p e r a t i o n s : p u


  1. S t a c k s a n d q u e u e s ( W e i s s c h a p t e r 3 )

  2. 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 M a 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 t o t h e s t a c k ● p o p ( ) – r e m o v e t h e m o s t - r e c e n t l y - p u s h e d v a l u e f r o m t h e s t a c k L I F O : l a s t i n fj r s t o u t ● V a l u e r e m o v e d b y p o p i s a l w a y s t h e o n e t h a t w a s p u s h e d m o s t r e c e n t l y E x a m p l e : ● p u s h ( 1 ) ; p u s h ( 2 ) ; p o p ( ) ; p u s h ( 3 ) ; p o p ( ) ; p o p ( ) ● F i r s t p o p r e t u r n s 2 , s e c o n d p o p r e t u r n s 3 , t h i r d p o p r e t u r n s 1

  3. S t a c k s A n a l o g y f o r L I F O : s t a c k o f p l a t e s ● C a n o n l y a d d o r r e m o v e p l a t e s a t t h e t o p ! ● Y o u a l w a y s t a k e o fg t h e m o s t r e c e n t p l a t e

  4. S t a c k s M o r e s t a c k o p e r a t i o 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 o n t h e s t a c k ? ● t o p ( ) – r e t u r n m o s t - r e c e n t l y - p u s h e d ( “ t o p ” ) v a l u e w i t h o u t r e m o v i n g i t

  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 o ( h e l l o 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 o t i n g a t e x t t h a t c o n t a i n s a t y p o ( o r a r c h a i c [ a n d n o w a d a y s p e r h a p s w r o n g ] s p e l l i n g ) t o s h o w t h a t t h e m i s t a k e w a s i n t h e o r i g i n a l t e x t ( a n d n o t i n t r o d u c e d w h i l e c o p y i n g t h e q u o 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 o p e n i n g b r a c k e t h a s a c l o s i n g b r a c k e t ● E v e r y c l o s i n g b r a c k e t h a s a n o 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 o “ ( [ ) ] ” !

  6. A l g o r i t h m M a i n t a i n a s t a c k o f o 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 o t h r o u g h s t r i n g o 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 o 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 o s i n g b r a c k e t , p o p f r o 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 o p p e d v a l u e i s a “ ( “ ● Wh e n w e g e t t o t h e e n d o 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

  7. A l g o r i t h m C h e c k y o u r u n d e r s t a n d i n g : Wh a t h a s g o n e w r o n g i f e a c h o f t h e s t e p s M a i n t a i n a s t a c k o f o p e n e d b r a c k e t s w r i t t e n i n b o l d f a i l s ? ● I n i t i a l l y s t a c k i s e m p t y ● G o t h r o u g h s t r i n g o 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 o 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 o s i n g b r a c k e t , p o p f r o 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 ( p o p f a i l s i f s t a c k i s e m p t y ) – e . g . , i f w e s e e a “ ) ” , c h e c k t h a t t h e p o p p e d v a l u e i s a “ ( “ ● Wh e n w e g e t t o t h e e n d o 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

  8. M o r e u s e s o 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 o c e s s o r t o h a n d l e f u n c t i o n c a l l s ● Wh e n y o u c a l l a f u n c t i o n , t h e p r o c e s s o r r e c o r d s w h a t i t w a s d o i n g b y p u s h i n g a r e c o r d o n t o t h e c a l l s t a c k ● Wh e n a f u n c t i o n r e t u r n s , t h e p r o c e s s o r p o p s a r e c o r d o fg t h e c a l l s t a c k t o s e e w h a t i t s h o u l d c a r r y o n d o i n g P a r s i n g i n c o m p i l e r s L o t s o f u s e s i n a l g o r i t h m s !

  9. 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 o t h e e n d o f t h e a r r a y ● P o p : r e m o v e e l e m e n t f r o m t h e e n d o f t h e a r r a y C o m p l e x i t y : a l l o p e r a t i o n s h a v e a m o r t i s e d O ( 1 ) c o m p l e x i t y ● M e a n s : n o p e r a t i o n s t a k e O ( n ) t i m e ● A l t h o u g h a s i n g l e o p e r a t i o 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 ” o p e r a t i o n i s a l w a y s b a l a n c e d o u t b y a l o t o f e a r l i e r “ c h e a p ” o p e r a t i o n s

  10. A b s t r a c t d a t a t y p e s Y o u s h o 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 o n ( e . g . a d y n a m i c a r r a y ) Wh y ? ● Wh e n y o u u s e a d a t a s t r u c t u r e y o u d o n ' t c a r e h o w i t ' s i m p l e m e n t e d ● Y o u r c o d e a n d d e s i g n w i l l b e c l e a r e r i f y o u u s e A D T s ● M o s t A D T s h a v e m a n y p o s s i b l e i m p l e m e n t a t i o n s

  11. Q u e u e s A q u e u e a l s o s t o r e s a s e q u e n c e o f v a l u e s M a i n o p e r a t i o n s : ● e n q u e u e ( x ) – a d d v a l u e x t o t h e q u e u e ● d e q u e u e ( ) – r e m o 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 o 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 o l d e s t o n e t h a t ' s s t i l l i n t h e q u e u e M u c h l i k e a s t a c k – b u t F I F O , n o t L I F O

  12. 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 o e n t e r t h e q u e u e i s t h e fj r s t t o l e a v e

  13. U s e s o f q u e u e s C o n t r o l l i n g a c c e s s t o s h a r e d r e s o u r c e s i n a n o 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 o f r e q u e s t s i n a w e b s e r v e r ● G e n e r a l l y , m e s s a g e q u e u e s a r e u s e d t o s e n d i n f o r m a t i o n b e t w e e n p r o c e s s e s i n c o n c u r r e n t p r o g r a m s A l s o a p p e a r s i n l o t s o f a l g o r i t h m s ● ( S t a c k s a n d q u e u e s b o t h a p p e a r w h e n a n a l g o r i t h m h a s t o r e m e m b e r a l i s t o f t h i n g s t o d o )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend