Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Stacks in C# - Comprehensive Tutorial

Introduction to Stacks

A stack is a collection of elements that follows the Last In First Out (LIFO) principle. The element added last is the one to be removed first. This data structure is analogous to a stack of plates where you can only take the top plate off the stack or add a new plate on the top.

Basic Operations

Stacks primarily have the following operations:

  • Push: Adds an element to the top of the stack.
  • Pop: Removes and returns the top element of the stack.
  • Peek: Returns the top element without removing it from the stack.
  • IsEmpty: Checks if the stack is empty.

Creating a Stack in C#

In C#, the Stack class is available in the System.Collections.Generic namespace. Below is an example of how to create and use a stack in C#.

using System;
using System.Collections.Generic;

// Stack example

class Program
{
    static void Main(string[] args)
    {
        Stack stack = new Stack();

        // Push elements
        stack.Push(1);
        stack.Push(2);
        stack.Push(3);

        // Peek the top element
        Console.WriteLine("Peek: " + stack.Peek());

        // Pop elements
        Console.WriteLine("Pop: " + stack.Pop());
        Console.WriteLine("Pop: " + stack.Pop());
        Console.WriteLine("Pop: " + stack.Pop());

        // Check if the stack is empty
        Console.WriteLine("Is stack empty? " + stack.Count == 0);
    }
}

Detailed Example

Let's take a more detailed example to understand the stack operations better.

using System;
using System.Collections.Generic;

// Detailed stack example

class Program
{
    static void Main(string[] args)
    {
        Stack stack = new Stack();

        // Push elements
        stack.Push("First");
        stack.Push("Second");
        stack.Push("Third");

        // Iterate through the stack
        foreach (var item in stack)
        {
            Console.WriteLine(item);
        }

        // Peek the top element
        Console.WriteLine("Peek: " + stack.Peek());

        // Pop elements
        Console.WriteLine("Pop: " + stack.Pop());
        Console.WriteLine("Pop: " + stack.Pop());
        Console.WriteLine("Pop: " + stack.Pop());

        // Check if the stack is empty
        Console.WriteLine("Is stack empty? " + stack.Count == 0);
    }
}

First
Second
Third
Peek: Third
Pop: Third
Pop: Second
Pop: First
Is stack empty? True

Use Cases of Stacks

Stacks are used in various applications such as:

  • Expression evaluation and syntax parsing
  • Backtracking algorithms
  • Memory management (e.g., function call stack in programming languages)
  • Undo mechanisms in text editors

Conclusion

Stacks are a fundamental data structure used in many different areas of computer science and software engineering. Understanding how to implement and use stacks in C# will enhance your problem-solving skills and help you in various applications that require LIFO behavior.