07 Feb 2024




Intermediate

Stack Generic Collections in C#

Definition:

Stack<T> is a generic collection class in the System.Collections.Generic namespace of C#. It represents a last-in, first-out (LIFO) data structure, where elements are added and removed from the top of the stack. The T represents the type of elements stored in the stack.

Key Features and Characteristics:

  • LIFO Structure: Elements are added to and removed from the top of the stack. The last element added is the first to be removed.
  • Generic Type Parameter: Stack is generic, allowing it to store elements of any data type specified by the developer.
  • Dynamic Sizing: Similar to List, Stack can dynamically resize itself to accommodate more elements as needed.
  • Push and Pop Operations: Elements are added to the top of the stack using the Push method and removed from the top using the Pop method.
  • Peek Operation: The Peek method allows you to view the element at the top of the stack without removing it.
  • Count Property: The Count property provides the number of elements currently in the stack.
  • Clear Method: The Clear method removes all elements from the stack.

Uses:

  • Modeling scenarios where elements are accessed and processed in reverse order of their insertion.
  • Implementing algorithms such as depth-first search (DFS), backtracking, and expression evaluation.
  • Tracking state changes or undo operations in applications.

Advantages:

  • Efficient for scenarios where elements are processed in the reverse order of their insertion.
  • Provides a straightforward way to manage a stack of items with built-in methods for pushing and popping elements.
  • Dynamic resizing capability allows Stack to efficiently handle varying numbers of elements.

Disadvantages:

  • Not suitable for scenarios where random access to elements is required.
  • Insertion or removal of elements in the middle of the stack is not efficient.

Code Example:

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Creating a Stack of integers
        Stack<int> numberStack = new Stack<int>();

        // Pushing elements onto the stack
        numberStack.Push(1);
        numberStack.Push(2);
        numberStack.Push(3);

        // Popping elements from the stack
        int poppedNumber = numberStack.Pop();
        Console.WriteLine("Popped: " + poppedNumber); // Output: Popped: 3

        // Peeking at the top element without popping
        int peekedNumber = numberStack.Peek();
        Console.WriteLine("Peeked: " + peekedNumber); // Output: Peeked: 2

        // Displaying the remaining elements in the stack
        Console.WriteLine("Remaining Elements in the Stack:");
        foreach (var number in numberStack)
        {
            Console.WriteLine(number);
        }
    }
}

Explanation: This example demonstrates creating a Stack<int> and pushing three numbers onto it. The Push method is used to add numbers to the top of the stack. The Pop method removes and returns the top element from the stack. The Peek method allows viewing the top element without removing it. Finally, a foreach loop is used to iterate over the remaining elements in the stack and print them.

c-sharp
stack
generic-collections