Pass generic Stack as parameter - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Stack

Description

Pass generic Stack as parameter

Demo Code

using System;//from   ww  w  . java 2  s. com
using System.Collections.Generic;
public class EmptyStackException : Exception
{
    // parameterless constructor
    public EmptyStackException() : base("Stack is empty")
    {
    }
    // one-parameter constructor
    public EmptyStackException(string exception) : base(exception)
    {
    }
    // two-parameter constructor
    public EmptyStackException(string exception, Exception inner)
    : base(exception, inner)
    {
    }
}
public class FullStackException : Exception
{
    // parameterless constructor
    public FullStackException() : base("Stack is full")
    {
    }
    // one-parameter constructor
    public FullStackException(string exception) : base(exception)
    {
    }
    // two-parameter constructor
    public FullStackException(string exception, Exception inner)
    : base(exception, inner)
    {
    }
}
public class Stack<T>
{
    private int top; // location of the top element
    private T[] elements; // array that stores stack elements
                          // parameterless constructor creates a stack of the default size
    public Stack()
    : this(10) // default stack size
    {
    }
    // constructor creates a stack of the specified number of elements
    public Stack(int stackSize)
    {
        if (stackSize <= 0) // validate stackSize
        {
            throw new ArgumentException("Stack size must be positive.");
        }
        elements = new T[stackSize]; // create stackSize elements
        top = -1; // stack initially empty
    }
    public void Push(T pushValue)
    {
        if (top == elements.Length - 1) // stack is full
        {
            throw new FullStackException(
            $"Stack is full, cannot push {pushValue}");
        }
        ++top; // increment top
        elements[top] = pushValue; // place pushValue on stack
    }
    // return the top element if not empty,
    // else throw EmptyStackException
    public T Pop()
    {
        if (top == -1) // stack is empty
        {
            throw new EmptyStackException("Stack is empty, cannot pop");
        }
        --top; // decrement top
        return elements[top + 1]; // return top value
    }
}
class StackTest
{
    // create arrays of doubles and ints
    private static double[] doubleElements =
    {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
    private static int[] intElements =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    private static Stack<double> doubleStack; // stack stores doubles
    private static Stack<int> intStack; // stack stores ints
    static void Main()
    {
        doubleStack = new Stack<double>(5); // stack of doubles
        intStack = new Stack<int>(10); // stack of ints
        TestPush(nameof(doubleStack), doubleStack, doubleElements);
        TestPop(nameof(doubleStack), doubleStack);
        TestPush(nameof(doubleStack), intStack, intElements);
        TestPop(nameof(doubleStack), intStack);
    }
    private static void TestPush<T>(string name, Stack<T> stack, IEnumerable<T> elements)
    {
        try
        {
            Console.WriteLine($"\nPushing elements onto {name}");
            foreach (var element in elements)
            {
                Console.Write($"{element} ");
                stack.Push(element); // push onto stack
            }
        }
        catch (FullStackException exception)
        {
            Console.Error.WriteLine($"\nMessage: {exception.Message}");
            Console.Error.WriteLine(exception.StackTrace);
        }
    }
    private static void TestPop<T>(string name, Stack<T> stack)
    {
        try
        {
            Console.WriteLine($"\nPopping elements from {name}");
            T popValue; // store element removed from stack
            while (true)
            {
                popValue = stack.Pop(); // pop from stack
                Console.Write($"{popValue} ");
            }
        }
        catch (EmptyStackException exception)
        {
            Console.Error.WriteLine($"\nMessage: {exception.Message}");
            Console.Error.WriteLine(exception.StackTrace);
        }
    }
}

Result


Related Tutorials