
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Reverse a Stack using C#
Set a stack and add elements to it.
Stack st = new Stack(); st.Push('P'); st.Push('Q'); st.Push('R');
Now set another stack to reverse it.
Stack rev = new Stack();
Until the count of ths Stack is not equal to 0, use the Push and Pop method to reverse it.
while (st.Count != 0) { rev.Push(st.Pop()); }
The following is the complete code −
Example
using System; using System.Collections; namespace CollectionsApplication { 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("Current 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 + " "); } } } }
Output
Current stack: R Q P Reversed stack: P Q R
- Related Articles
- Reverse a number using stack in C++
- Reverse a link list using stack in C++
- Reverse a Stack using Queue
- Print Reverse a linked list using Stack
- Python Program to Reverse a Stack using Recursion
- Golang program to reverse a stack
- How to reverse the elements of an array using stack in java?
- C# Program to Reverse a String without using Reverse() Method
- Reverse a Linked List using C++
- How to reverse a String using C#?
- Reverse a Doubly Linked List using C++
- Implement Stack using Queues in C++
- Reverse an array using C#
- C++ program to Reverse a Sentence Using Recursion
- Reverse a string using the pointer in C++

Advertisements