Growable array based stack | constant amount at which stack is increased ; top of the stack ; length of stack ; function to create new stack ; allocate memory for new stack ; copying the content of old stack ; re - sizing the length ; function to push new element ; if stack is full , create new one ; insert element at top of the stack ; function to pop an element ; function to display ; if top is - 1 , that means stack is empty ; Driver Code ; creating initial stack ; pushing element to top of stack ; pushing more element when stack is full ; pushing more element so that stack can grow
BOUND = 4 NEW_LINE top = - 1 ; NEW_LINE a = [ ] NEW_LINE length = 0 ; NEW_LINE def create_new ( ) : NEW_LINE INDENT global length ; NEW_LINE new_a = [ 0 for i in range ( length + BOUND ) ] ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT new_a [ i ] = a [ i ] ; NEW_LINE DEDENT length += BOUND ; NEW_LINE return new_a NEW_LINE DEDENT def push ( element ) : NEW_LINE INDENT global top , a NEW_LINE if ( top == length - 1 ) : NEW_LINE INDENT a = create_new ( ) ; NEW_LINE DEDENT top += 1 NEW_LINE a [ top ] = element ; NEW_LINE return a ; NEW_LINE DEDENT def pop ( ) : NEW_LINE INDENT global top NEW_LINE top -= 1 ; NEW_LINE DEDENT def display ( ) : NEW_LINE INDENT global top NEW_LINE if ( top == - 1 ) : NEW_LINE INDENT print ( " Stack ▁ is ▁ Empty " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Stack : ▁ " , end = ' ' ) NEW_LINE for i in range ( top + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = create_new ( ) ; NEW_LINE push ( 1 ) ; NEW_LINE push ( 2 ) ; NEW_LINE push ( 3 ) ; NEW_LINE push ( 4 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 5 ) ; NEW_LINE push ( 6 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 7 ) ; NEW_LINE push ( 8 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 9 ) ; NEW_LINE display ( ) ; NEW_LINE DEDENT