Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Reverse a Stack using C#
A stack is a Last-In-First-Out (LIFO) data structure in C#. To reverse a stack, we can use another stack and leverage the LIFO property by popping elements from the original stack and pushing them into a new stack.
The System.Collections.Stack class provides Push() and Pop() methods that make stack reversal straightforward.
Syntax
Following is the basic syntax for creating and reversing a stack −
Stack originalStack = new Stack();
Stack reversedStack = new Stack();
while (originalStack.Count != 0) {
reversedStack.Push(originalStack.Pop());
}
How Stack Reversal Works
The reversal process works by exploiting the LIFO nature of stacks. When we pop elements from the original stack and push them into a new stack, the order gets reversed.
Using Another Stack for Reversal
Example
using System;
using System.Collections;
public class Program {
public static void Main(string[] args) {
Stack st = new Stack();
Stack rev = new Stack();
st.Push('P');
st.Push('Q');
st.Push('R');
Console.WriteLine("Original stack:");
foreach(char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
while (st.Count != 0) {
rev.Push(st.Pop());
}
Console.WriteLine("Reversed stack:");
foreach(char c in rev) {
Console.Write(c + " ");
}
}
}
The output of the above code is −
Original stack: R Q P Reversed stack: P Q R
Using Recursion for In-Place Reversal
We can also reverse a stack in-place using recursion without using an additional stack −
Example
using System;
using System.Collections;
public class Program {
public static void ReverseStack(Stack st) {
if (st.Count <= 1) return;
object temp = st.Pop();
ReverseStack(st);
InsertAtBottom(st, temp);
}
public static void InsertAtBottom(Stack st, object item) {
if (st.Count == 0) {
st.Push(item);
return;
}
object temp = st.Pop();
InsertAtBottom(st, item);
st.Push(temp);
}
public static void Main(string[] args) {
Stack st = new Stack();
st.Push('A');
st.Push('B');
st.Push('C');
Console.WriteLine("Original stack:");
foreach(char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
ReverseStack(st);
Console.WriteLine("Reversed stack:");
foreach(char c in st) {
Console.Write(c + " ");
}
}
}
The output of the above code is −
Original stack: C B A Reversed stack: A B C
Comparison of Methods
| Method | Space Complexity | Time Complexity | Advantages |
|---|---|---|---|
| Using Another Stack | O(n) | O(n) | Simple and easy to understand |
| Using Recursion | O(n) | O(n²) | In-place reversal, no additional stack needed |
Conclusion
Reversing a stack in C# can be accomplished using another stack for O(n) time complexity, or recursively for in-place reversal. The additional stack method is more efficient and commonly used, while recursion provides an elegant solution without extra data structures.
