Delete middle element of a stack | Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver function to test above functions ; push elements into the stack ; Printing stack after deletion of middle .
class Stack : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . items = [ ] NEW_LINE DEDENT def isEmpty ( self ) : NEW_LINE INDENT return self . items == [ ] NEW_LINE DEDENT def push ( self , item ) : NEW_LINE INDENT self . items . append ( item ) NEW_LINE DEDENT def pop ( self ) : NEW_LINE INDENT return self . items . pop ( ) NEW_LINE DEDENT def peek ( self ) : NEW_LINE INDENT return self . items [ len ( self . items ) - 1 ] NEW_LINE DEDENT def size ( self ) : NEW_LINE INDENT return len ( self . items ) NEW_LINE DEDENT DEDENT def deleteMid ( st , n , curr ) : NEW_LINE INDENT if ( st . isEmpty ( ) or curr == n ) : NEW_LINE INDENT return NEW_LINE DEDENT x = st . peek ( ) NEW_LINE st . pop ( ) NEW_LINE deleteMid ( st , n , curr + 1 ) NEW_LINE if ( curr != int ( n / 2 ) ) : NEW_LINE INDENT st . push ( x ) NEW_LINE DEDENT DEDENT st = Stack ( ) NEW_LINE st . push ( '1' ) NEW_LINE st . push ( '2' ) NEW_LINE st . push ( '3' ) NEW_LINE st . push ( '4' ) NEW_LINE st . push ( '5' ) NEW_LINE st . push ( '6' ) NEW_LINE st . push ( '7' ) NEW_LINE deleteMid ( st , st . size ( ) , 0 ) NEW_LINE while ( st . isEmpty ( ) == False ) : NEW_LINE INDENT p = st . peek ( ) NEW_LINE st . pop ( ) NEW_LINE print ( str ( p ) + " ▁ " , end = " " ) NEW_LINE DEDENT