/// Test the generic stack
    public static void TestGenericStack()
    {
        // Create stack
        int size = 10;
        GenericStack <double> stack  = new GenericStack <double>(size);
        GenericStack <string> stack2 = new GenericStack <string>(size);

        // Push elements on the stack
        try
        {
            for (int i = 0; i <= size; i++)
            {
                stack.Push(i);
                Console.WriteLine("Push: {0}", i);
            }
        }
        catch (ApplicationException ex)
        {
            Console.WriteLine("Error while pushing values on the stack: {0}", ex.Message);
        }

        // Pop elements from the stack
        double total = 0.0;

        try
        {
            for (int i = 0; i <= size + 5; i++)
            {
                // Note, no casting needed.
                double value = stack.Pop();
                total += value;
                Console.WriteLine("Pop: {0}", value);
            }
        }
        catch (ApplicationException ex)
        {
            Console.WriteLine("Error while poping values from the stack: {0}", ex.Message);
        }

        Console.WriteLine("Total: {0}", total);

        // Using Generic methods
        int sz1 = 10; int sz2 = 6;
        GenericStack <double> stackA = new GenericStack <double>(sz1);
        GenericStack <double> stackB = new GenericStack <double>(sz2);

        GenericMethod.Swap <GenericStack <double> >(ref stackA, ref stackB);
        Console.WriteLine("Sizes of stacks: {0} {1}", stackA.Size(), stackB.Size());

        // Swap 2 doubles
        double d1 = 1.2; double d2 = 3.0;

        GenericMethod.Swap <double>(ref d1, ref d2);
        Console.WriteLine("Sizes of stacks: {0} {1}", d1, d2);
    }
Пример #2
0
        public void Push_WhenGivenOneInt_ShoudReturn1()
        {
            //Act
            var stack = new GenericStack <int>(1);

            stack.Push(5);
            var expected = 1;

            //Assert
            Assert.AreEqual(expected, stack.Size());
        }
Пример #3
0
        static void Main(string[] args)
        {
            int size = 5;

            GenericStack <int> EnterThePile = new GenericStack <int>(size);

            EnterThePile.IsStackEmpty();

            Console.WriteLine("\n");

            EnterThePile.Push(5);
            EnterThePile.Push(2);
            EnterThePile.Push(3);
            EnterThePile.Push(8);
            EnterThePile.Push(1);

            EnterThePile.IsStackEmpty();

            Console.WriteLine("\n");

            EnterThePile.PrintStack();

            Console.WriteLine("\n");

            EnterThePile.PrintStack();

            Console.WriteLine("\n");

            EnterThePile.Push(10);

            Console.WriteLine("\n");

            EnterThePile.Size();

            EnterThePile.Top();

            EnterThePile.Pop();

            EnterThePile.ReverseStack();

            EnterThePile.PrintStack();

            Console.WriteLine("\n");

            Console.ReadLine();
        }