
- 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
C# Program to Implement Stack with Push and Pop operations
Set stack with Push operation to add elements to the Stack −
Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W');
To Pop elements from the stack, use Pop() method −
st.Pop();
st.Pop();
The following is an example to implement a stack with Push and Pop operations −
Example
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('V'); st.Push('H'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values "); st.Pop(); st.Pop(); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } } } }
Output
Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A
- Related Articles
- stack push() and pop() in C++ STL
- Push vs pop in stack class in C#
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- C++ Program to Implement Stack
- Program to construct Maximum Stack with given operations in C++
- Python Program to Implement a Stack
- queue::push() and queue::pop() in C++ STL
- Array push(), pop() and clear() functions in Ruby
- C++ Program to Implement Stack using array
- C++ Program to Implement Graph Structured Stack
- C++ Program to Implement Stack in STL
- Golang program to implement stack data structure
- Validating push pop sequence in JavaScript
- Count the number of pop operations on stack to get each element of the array in C++
- How to use Push-Location and Pop-Location command in PowerShell?

Advertisements