Create generic Stack - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Stack

Description

Create generic Stack

Demo Code

using System;/*from w  w w . ja v  a 2  s.  c o  m*/
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
// creates a stack of the default size
public Stack() : this(10) // default stack size
{
}
// 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
}
// push element onto the stack; if unsuccessful, throw FullStackException
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
{
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
TestPushDouble(); // push doubles onto doubleStack
TestPopDouble(); // pop doubles from doubleStack
TestPushInt(); // push ints onto intStack
TestPopInt(); // pop ints from intStack
}
private static void TestPushDouble()
{
try
{
Console.WriteLine("\nPushing elements onto doubleStack");
foreach (var element in doubleElements)
{
Console.Write($"{element:F1} ");
doubleStack.Push(element); // push onto doubleStack
}
}
catch (FullStackException exception)
{
Console.Error.WriteLine($"\nMessage: {exception.Message}");
Console.Error.WriteLine(exception.StackTrace);
}
}
private static void TestPopDouble()
{
try
{
Console.WriteLine("\nPopping elements from doubleStack");
double popValue; // store element removed from stack
while (true)
{
popValue = doubleStack.Pop(); // pop from doubleStack
Console.Write($"{popValue:F1} ");
}
}
catch (EmptyStackException exception)
{
Console.Error.WriteLine($"\nMessage: {exception.Message}");
Console.Error.WriteLine(exception.StackTrace);
}
}
private static void TestPushInt()
{
try
{
Console.WriteLine("\nPushing elements onto intStack");
foreach (var element in intElements)
{
Console.Write($"{element} ");
intStack.Push(element); // push onto intStack
}
}
catch (FullStackException exception)
{
Console.Error.WriteLine($"\nMessage: {exception.Message}");
Console.Error.WriteLine(exception.StackTrace);
}
}
private static void TestPopInt()
{
try
{
Console.WriteLine("\nPopping elements from intStack");
int popValue; // store element removed from stack
while (true)
{
popValue = intStack.Pop(); // pop from intStack
Console.Write($"{popValue:F1} ");
}
}
catch (EmptyStackException exception)
{
Console.Error.WriteLine($"\nMessage: {exception.Message}");
Console.Error.WriteLine(exception.StackTrace);
}
}
}

Result


Related Tutorials