Exemple #1
0
        static void Main(string[] args)
        {
            Stack<string> books = new Stack<string>();

            books.Push("Programming WPF");
            books.Push("The Philosophy Book");
            books.Push("Heat and Thermodynamics");
            books.Push("Harry Potter and the Chamber of Secrets");

            Console.WriteLine("ALL BOOKS:");
            foreach (string title in books)
            {
                Console.WriteLine(title);
            }

            string topItem = books.Pop();
            string topItem = books.Peek();  // Allows you to peek at the top item and not remove it
            Console.WriteLine("\r\nTOP ITEM IS: " + topItem);

            Console.WriteLine("\r\nALL BOOKS:");
            foreach (string title in books)
            {
                Console.WriteLine(title);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var numberOfQueries = int.Parse(Console.ReadLine());

            var stack = new Stack<int>();
            var maxStack = new Stack<int>();
            maxStack.Push(int.MinValue);

            for (int i = 0; i < numberOfQueries; i++)
            {
                var parameters = Console.ReadLine().Split().Select(int.Parse).ToArray();
                var operation = parameters[0];

                switch (operation)
                {
                    case 1:
                        var number = parameters[1];
                        stack.Push(number);
                        if (stack.Peek() > maxStack.Peek())
                        {
                            maxStack.Push(stack.Peek());
                        }
                        break;
                    case 2:
                        var num = stack.Pop();
                        if (num == maxStack.Peek())
                        {
                            maxStack.Pop();
                        }
                        break;
                    case 3:
                        Console.WriteLine(maxStack.Peek());
                        break;
                    default:
                        break;
                }
            }
        }