1.栈的性质(后进先出)
s = [ ]
s. append( 'https://www.cnn.com/' )
s. append( 'https://www.cnn.com/world' )
s. append( 'https://www.cnn.com/india' )
s. append( 'https://www.cnn.com/china' )
s
s. pop( )
s. pop( )
s
s[ - 1 ]
2.使用deque作为栈
stack. append( 'https://www.cnn.com/' )
stack. append( 'https://www.cnn.com/world' )
stack. append( 'https://www.cnn.com/india' )
stack. append( 'https://www.cnn.com/china' )
stack
stack. pop( )
stack. pop( )
stack
stack. pop( )
stack. pop( )
stack. pop( )
3.使用双端队列实现Stack类
class Stack :
def __init__ ( self) :
self. container = deque( )
def push ( self, val) :
self. container. append( val)
def pop ( self) :
return self. container. pop( )
def peek ( self) :
return self. container[ - 1 ]
def is_empty ( self) :
return len ( self. container) == 0
def size ( self) :
return len ( self. container)
s = Stack( )
s. push( 5 )
s. is_empty( )
s. pop( )
s. is_empty( )
s. push( 9 )
s. push( 34 )
s. push( 78 )
s. push( 12 )
s. peek( )