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
Push vs pop in stack class in C#
The Stack class in C# represents a last-in, first-out (LIFO) collection of objects. It is used when you need to store and retrieve elements in reverse order ? the last element added is the first one to be removed.
The Stack class provides two fundamental operations: Push() to add elements and Pop() to remove elements from the top of the stack.
Syntax
Following is the syntax for Push and Pop operations −
// Push operation - adds element to top stack.Push(element); // Pop operation - removes and returns top element object topElement = stack.Pop(); // Peek operation - returns top element without removing object topElement = stack.Peek();
Key Properties and Methods
-
Count − Gets the number of elements in the stack.
-
Push(object) − Adds an element to the top of the stack.
-
Pop() − Removes and returns the top element from the stack.
-
Peek() − Returns the top element without removing it.
Using Push Operation
The Push operation adds elements to the top of the stack. Each new element becomes the top element −
using System;
using System.Collections;
class Program {
static void Main() {
Stack st = new Stack();
st.Push('A');
st.Push('B');
st.Push('C');
st.Push('D');
Console.WriteLine("Elements after Push operations:");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Count: " + st.Count);
}
}
The output of the above code is −
Elements after Push operations: D C B A Count: 4
Using Pop Operation
The Pop operation removes and returns the top element from the stack −
using System;
using System.Collections;
class Program {
static void Main() {
Stack st = new Stack();
st.Push('A');
st.Push('B');
st.Push('C');
st.Push('D');
Console.WriteLine("Original stack:");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Popping elements:");
Console.WriteLine("Popped: " + st.Pop());
Console.WriteLine("Popped: " + st.Pop());
Console.WriteLine("Remaining stack:");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Count: " + st.Count);
}
}
The output of the above code is −
Original stack: D C B A Popping elements: Popped: D Popped: C Remaining stack: B A Count: 2
Complete Example with Push, Pop, and Peek
using System;
using System.Collections;
class Program {
static void Main() {
Stack st = new Stack();
st.Push('A');
st.Push('B');
st.Push('C');
st.Push('D');
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
st.Push('P');
st.Push('Q');
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 + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Current stack: D C B A The next poppable value in stack: Q Current stack: Q P D C B A Removing values.... Current stack: C B A
Push vs Pop Comparison
| Operation | Purpose | Effect on Stack | Returns |
|---|---|---|---|
| Push | Add element to stack | Increases stack size | void |
| Pop | Remove top element | Decreases stack size | Removed element |
| Peek | View top element | No change to stack | Top element |
Conclusion
Push and Pop are the fundamental operations of a Stack in C#. Push adds elements to the top while Pop removes elements from the top, following the LIFO principle. Use Push to store data and Pop to retrieve it in reverse order of insertion.
