コード例 #1
0
 private static void PrintStack(StackCustom<int> stack)
 {
     foreach (var num in stack)
     {
         Console.Write(num + " ");
     }
     Console.WriteLine();
 }
コード例 #2
0
 private static void PrintStack(StackCustom <int> stack)
 {
     foreach (var num in stack)
     {
         Console.Write(num + " ");
     }
     Console.WriteLine();
 }
コード例 #3
0
    static void Main()
    {
        Console.WriteLine("Testing custom stack class.");
        Console.WriteLine("Class features:");
        Console.WriteLine(" - array implementation;");
        Console.WriteLine(" - methods Push, Pop, Peek, Contains and property Count.");
        Console.WriteLine();
        Console.WriteLine("Creating an instance of the class Stack(custom):");
        StackCustom <int> stack = new StackCustom <int>();

        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Using Push(), insert numbers from 1 to 10");
        for (int i = 1; i <= 10; i++)
        {
            stack.Push(i);
        }
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Using custom enumerator, print the content of the stack:");
        PrintStack(stack);
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Print the count of elements: " + stack.Count);
        Console.WriteLine("Peek at top element: " + stack.Peek());
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Remove top 5 elements using Pop():");
        for (int i = 0; i < 5; i++)
        {
            stack.Pop();
        }
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Print the count of elements: " + stack.Count);
        Console.WriteLine("Peek at top element: " + stack.Peek());
        PrintStack(stack);
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Using Push(), insert numbers from 1 to 10");
        for (int i = 1; i <= 10; i++)
        {
            stack.Push(i);
        }
        Console.WriteLine("Print the stack again:");
        PrintStack(stack);
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Check if stack contains the number -15: " + stack.Contains(-15));
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Check if stack contains the number 10: " + stack.Contains(10));
        Console.WriteLine("DONE!" + Environment.NewLine);
        Console.WriteLine("Clear the stack using Clear().");
        stack.Clear();
        try
        {
            Console.WriteLine("Peek at top element: ");
            stack.Peek();
        }
        catch (InvalidOperationException e)
        {
            Console.WriteLine(e.Message);
        }
    }
コード例 #4
0
 static void Main()
 {
     Console.WriteLine("Testing custom stack class.");
     Console.WriteLine("Class features:");
     Console.WriteLine(" - array implementation;");
     Console.WriteLine(" - methods Push, Pop, Peek, Contains and property Count.");
     Console.WriteLine();
     Console.WriteLine("Creating an instance of the class Stack(custom):");
     StackCustom<int> stack = new StackCustom<int>();
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Using Push(), insert numbers from 1 to 10");
     for (int i = 1; i <= 10; i++)
     {
         stack.Push(i);
     }
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Using custom enumerator, print the content of the stack:");
     PrintStack(stack);
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Print the count of elements: " + stack.Count);
     Console.WriteLine("Peek at top element: " + stack.Peek());
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Remove top 5 elements using Pop():");
     for (int i = 0; i < 5; i++)
     {
         stack.Pop();
     }
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Print the count of elements: " + stack.Count);
     Console.WriteLine("Peek at top element: " + stack.Peek());
     PrintStack(stack);
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Using Push(), insert numbers from 1 to 10");
     for (int i = 1; i <= 10; i++)
     {
         stack.Push(i);
     }
     Console.WriteLine("Print the stack again:");
     PrintStack(stack);
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Check if stack contains the number -15: " + stack.Contains(-15));
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Check if stack contains the number 10: " + stack.Contains(10));
     Console.WriteLine("DONE!" + Environment.NewLine);
     Console.WriteLine("Clear the stack using Clear().");
     stack.Clear();
     try
     {
         Console.WriteLine("Peek at top element: ");
         stack.Peek();
     }
     catch (InvalidOperationException e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #5
0
        private static void Main()
        {
            var stackCustom = new StackCustom<string>();

            stackCustom.Push("Ivan");
            stackCustom.Push("Pesho");
            stackCustom.Push("Dimitar");
            stackCustom.Push("Maria");

            Console.WriteLine("Top = {0}", stackCustom.Peak());

            while (stackCustom.Count > 0)
            {
                Console.WriteLine(stackCustom.Pop());
            }
        }