Stacks and Queues
Stacks and Queues
Stacks and Queues
Chandrabose Aravindan
<AravindanC@ssn.edu.in>
c l a s s AdapterStack :
d e f __init__ ( s e l f ) :
s e l f . _items = A r r a y L i s t ( )
c l a s s AdapterStack :
d e f __len__ ( s e l f ) :
r e t u r n ( l e n ( s e l f . _items ) )
d e f __str__ ( s e l f ) :
r e t u r n ( s t r ( s e l f . _items ) )
c l a s s AdapterStack :
def isEmpty ( s e l f ) :
r e t u r n ( l e n ( s e l f . _ i t e m s ) == 0 )
d e f push ( s e l f , e l e ) :
s e l f . _ i t e m s . append ( e l e )
c l a s s AdapterStack :
def top ( s e l f ) :
i f ( l e n ( s e l f . _ i t e m s ) == 0 ) :
r a i s e Empty ( " S t a c k ␣ i s ␣ empty ! " )
r e t u r n s e l f . _item . l a s t i t e m ( )
d e f pop ( s e l f ) :
i f ( l e n ( s e l f . _ i t e m s ) == 0 ) :
r a i s e Empty ( " S t a c k ␣ i s ␣ empty ! " )
r e t u r n s e l f . _ i t e m s . pop ( )
C. Aravindan (SNU) Data Structures April 09, 2024 10 / 36
Array-based Stack Implementation
import ctypes
c l a s s ArrayStack :
c l a s s ArrayStack :
d e f __len__ ( s e l f ) :
r e t u r n s e l f . _top
def isEmpty ( s e l f ) :
r e t u r n ( s e l f . _top == 0 )
c l a s s ArrayStack :
d e f push ( s e l f , i t e m ) :
i f ( s e l f . _top == s e l f . _ c a p a c i t y ) :
s e l f . _ r e s i z e (2 ∗ s e l f . _capacity )
s e l f . _ i t e m s [ s e l f . _top ] = i t e m
s e l f . _top += 1
c l a s s ArrayStack :
def top ( s e l f ) :
i f ( s e l f . _top == 0 ) :
r a i s e Empty ( " S t a c k ␣ i s ␣ empty ! " )
r e t u r n s e l f . _ i t e m s [ s e l f . _top − 1 ]
c l a s s ArrayStack :
d e f pop ( s e l f ) :
i f ( s e l f . _top == 0 ) :
r a i s e Empty ( " S t a c k ␣ i s ␣ empty ! " )
i t e m = s e l f . _ i t e m s [ s e l f . _top − 1 ]
s e l f . _top −= 1
s e l f . _ i t e m s [ s e l f . _top ] = None
i f ( s e l f . _top < ( s e l f . _ c a p a c i t y // 4 ) ) :
s e l f . _ r e s i z e ( s e l f . _ c a p a c i t y // 2 )
r e t u r n item
import ctypes
c l a s s CircArrayQueue :
We need a private method to find the ’next position’, when the array
is viewed as a ‘circular array’
We need a private method to find the ’next position’, when the array
is viewed as a ‘circular array’
c l a s s CircArrayQueue :
d e f _next ( pos ) :
r e t u r n ( ( pos + 1) % s e l f . _c ap ac it y )
c l a s s CircArrayQueue :
def i s F u l l ( s e l f ) :
r e t u r n ( s e l f . _ f r o n t == s e l f . _next ( s e l f . _ r e a r )
def isEmpty ( s e l f ) :
r e t u r n ( s e l f . _ f r o n t == s e l f . _ r e a r )
c l a s s CircArrayQueue :
d e f enqueue ( s e l f , i t e m ) :
i f ( s e l f . _ f r o n t == s e l f . _next ( s e l f . _ r e a r ) ) :
r a i s e F u l l ( " The␣ queue ␣ i s ␣ a l r e a d y ␣ f u l l ! " )
s e l f . _items [ s e l f . _rear ] = item
s e l f . _ r e a r = s e l f . _next ( s e l f . _ r e a r )
c l a s s CircArrayQueue :
def front ( s e l f ) :
i f ( s e l f . _ f r o n t == s e l f . _ r e a r ) :
r a i s e Empty ( " The␣ queue ␣ i s ␣ empty ! " )
r e t u r n s e l f . _items [ s e l f . _front ]
c l a s s CircArrayQueue :
d e f dequeue ( s e l f ) :
i f ( s e l f . _ f r o n t == s e l f . _ r e a r ) :
r a i s e Empty ( " The␣ queue ␣ i s ␣ empty ! " )
item = s e l f . _items [ s e l f . _front ]
s e l f . _ i t e m s [ s e l f . _ f r o n t ] = None
s e l f . _ f r o n t = s e l f . _next ( s e l f . _ f r o n t )
r e t u r n item
c l a s s CircArrayQueue :
d e f __len__ ( s e l f ) :
i f ( s e l f . _ f r o n t <= s e l f . _ r e a r ) :
return ( s e l f . _rear − s e l f . _front )
else :
return s e l f . _capacity − ( s e l f . _front − s e l
deq.addFirst(item)
deq.addLast(item)
deq.deleteFirst()
deq.deleteLast()
deq.first()
deq.last()
deq.isEmpty()
deq.isFull() [This is required only when the capacity is fixed]
len(deq)