C# Program to Implement Stack with Push and Pop operations

A Stack in C# is a Last-In-First-Out (LIFO) data structure that allows elements to be added and removed from only one end, called the top. The Stack class in the System.Collections namespace provides built-in methods for implementing stack operations.

The two primary operations of a stack are Push() to add elements and Pop() to remove elements. Additionally, the Peek() method allows you to view the top element without removing it.

Syntax

Following is the syntax for creating a Stack and using basic operations −

Stack stackName = new Stack();
stackName.Push(element);     // Add element to top
stackName.Pop();             // Remove and return top element
stackName.Peek();            // View top element without removing

Stack LIFO Operations W G M A Push Pop Top of Stack (Last In, First Out)

Using Push Operation

The Push() method adds elements to the top of the stack −

using System;
using System.Collections;

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("Stack 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 −

Stack after Push operations:
W G M A 
Count: 4

Using Pop Operation

The Pop() method removes and returns the top element from the stack −

using System;
using System.Collections;

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("Original stack:");
      foreach (char c in st) {
         Console.Write(c + " ");
      }
      Console.WriteLine();
      
      char poppedElement = (char)st.Pop();
      Console.WriteLine("Popped element: " + poppedElement);
      
      Console.WriteLine("Stack after Pop:");
      foreach (char c in st) {
         Console.Write(c + " ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Original stack:
W G M A 
Popped element: W
Stack after Pop:
G M A 

Complete Stack Implementation Example

The following example demonstrates a complete stack implementation with Push, Pop, and Peek operations −

using System;
using System.Collections;

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 + " ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

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 

Key Stack Methods

Method Description
Push(object) Adds an element to the top of the stack
Pop() Removes and returns the top element
Peek() Returns the top element without removing it
Count Gets the number of elements in the stack

Conclusion

The Stack class in C# provides an efficient implementation of the LIFO data structure. Use Push() to add elements to the top, Pop() to remove the top element, and Peek() to view the top element without removing it. Stacks are ideal for scenarios like undo operations, expression evaluation, and function call management.

Updated on: 2026-03-17T07:04:35+05:30

804 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements