Table of Contents

    Stack & Queue in C#

    Stack in C#

    A Stack is a collection of items that follows the Last In First Out (LIFO) principle. In a stack, the last element added is the first one to be removed. Common operations for a stack are:

    • Push: Add an item to the top of the stack.
    • Pop: Remove the item from the top of the stack.
    • Peek: View the top item without removing it.

    Here’s an example of how to use a stack in C#:

    
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Create a stack of integers
            Stack<int> stack = new Stack<int>();
    
            // Push elements to the stack
            stack.Push(10);
            stack.Push(20);
            stack.Push(30);
    
            // Peek at the top element
            Console.WriteLine("Top element is: " + stack.Peek());
    
            // Pop elements from the stack
            Console.WriteLine("Popped element: " + stack.Pop());
            Console.WriteLine("Popped element: " + stack.Pop());
    
            // Check the remaining elements
            Console.WriteLine("Top element is: " + stack.Peek());
    
            // Stack count after popping
            Console.WriteLine("Stack count: " + stack.Count);
        }
    }
    
    

    Output:

    
    Top element is: 30
    Popped element: 30
    Popped element: 20
    Top element is: 10
    Stack count: 1
    
    

    Queue in C#

    A Queue is a collection of items that follows the First In First Out (FIFO) principle. The first element added to the queue will be the first one to be removed. Common operations for a queue are:

    • Enqueue: Add an item to the end of the queue.
    • Dequeue: Remove the item from the front of the queue.
    • Peek: View the front item without removing it.

    Here’s an example of how to use a queue in C#:

    
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Create a queue of strings
            Queue<string> queue = new Queue<string>();
    
            // Enqueue elements to the queue
            queue.Enqueue("Alice");
            queue.Enqueue("Bob");
            queue.Enqueue("Charlie");
    
            // Peek at the front element
            Console.WriteLine("Front element is: " + queue.Peek());
    
            // Dequeue elements from the queue
            Console.WriteLine("Dequeued element: " + queue.Dequeue());
            Console.WriteLine("Dequeued element: " + queue.Dequeue());
    
            // Check the remaining elements
            Console.WriteLine("Front element is: " + queue.Peek());
    
            // Queue count after dequeuing
            Console.WriteLine("Queue count: " + queue.Count);
        }
    }
    
    

    Output:

    
    Front element is: Alice
    Dequeued element: Alice
    Dequeued element: Bob
    Front element is: Charlie
    Queue count: 1
    
    

    Key Differences:

    • Stack: Uses LIFO (Last In First Out) principle.
    • Queue: Uses FIFO (First In First Out) principle.

    Both structures are very useful depending on the problem you're trying to solve. A stack is good for scenarios like function calls or reversing items, while a queue is ideal for managing tasks in order or buffering data.