Пример #1
0
        //function for all stack operations
        static void StackOperation()
        {
            Stack stack = new Stack();
            int   choiceStack;

            do
            {
                Console.WriteLine();
                Console.WriteLine("Enter 1 to add a value in stack " +
                                  "\n2 to remove value " +
                                  "\n3 to show the stack " +
                                  "\n4 to get top value " +
                                  "\n5 to sort the stack" +
                                  "\n6 to Exit ");
                choiceStack = int.Parse(Console.ReadLine());
                switch (choiceStack)
                {
                case 1:
                    Console.WriteLine("Enter Elements");
                    int data = int.Parse(Console.ReadLine());
                    stack.Add(data);
                    Console.WriteLine("Data Added");
                    break;

                case 2: stack.Remove();
                    Console.WriteLine("Data removed");
                    break;

                case 3:
                    Console.Write("Stack contains :- ");
                    stack.Display();
                    Console.WriteLine();
                    break;

                case 4:
                    int top = stack.GetTopValue();
                    Console.WriteLine("Top of the stack is :- " + top);
                    Console.WriteLine();
                    break;

                case 5:
                    stack.Sort();
                    Console.WriteLine("Stack Sorted");
                    break;

                case 6:
                    break;

                default: Console.WriteLine("Invalid Entry");
                    break;
                }
            }while (choiceStack != 6);
        }
        public void InvokeMethods()
        {
            int choice = 0;

            Console.WriteLine("\t\tStack Operations\n1.Add\n2.Remove\n3.Display\n4.Sort\n5.Top\n6.Exit\nEnter your Choice : ");
            choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
            case 1:
                Console.WriteLine("Enter Data : ");
                _Element = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(_Stack.Add(_Element));
                InvokeMethods();
                break;

            case 2:
                Console.WriteLine(_Stack.Remove());
                InvokeMethods();
                break;

            case 3:
                Console.WriteLine(_Stack.Display());
                InvokeMethods();
                break;

            case 4:
                Console.WriteLine(_Stack.Sort());
                InvokeMethods();
                break;

            case 5:
                _Stack.Top();
                InvokeMethods();
                break;

            case 6:
                Environment.Exit(0);
                break;

            default:
                Console.WriteLine("Wrong choice, try again");
                break;
            }
        }