If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# code to delete middle of a stack without using the additional data structure with the output.
To increase your C# knowledge, practice all C# programs, here is a Collection of 100+ C# problems with solutions.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | using System; using System.Collections.Generic; class deleteMiddleofaStackProgram { 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 + " "); } } } |
7 6 5 3 2 1
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.