Delete middle element of a stack | C # code to delete middle of a stack without using additional data structure . ; 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 Code ; push elements into the stack ; Printing stack after deletion of middle .
using System ; using System . Collections . Generic ; class GFG { static void deleteMid ( Stack < char > st , int n , int curr = 0 ) { if ( st . Count == 0 curr == n ) return ; char x = st . Peek ( ) ; st . Pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . Push ( x ) ; } public static void Main ( ) { Stack < char > st = new Stack < char > ( ) ; st . Push ( '1' ) ; st . Push ( '2' ) ; st . Push ( '3' ) ; st . Push ( '4' ) ; st . Push ( '5' ) ; st . Push ( '6' ) ; st . Push ( '7' ) ; deleteMid ( st , st . Count ) ; while ( st . Count != 0 ) { char p = st . Peek ( ) ; st . Pop ( ) ; Console . Write ( p + " ▁ " ) ; } } }