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); } }
static void Main(string[] args) { var parameters = Console.ReadLine().Split().Select(int.Parse).ToArray(); var numberOfPushes = parameters[0]; var numberOfPops = parameters[1]; var numberToFind = parameters[2]; var inputNumbers = Console.ReadLine().Split().Select(int.Parse).ToArray(); var numberStack = new Stack<int>(); for (int i = 0; i < numberOfPushes; i++) { numberStack.Push(inputNumbers[i]); } for (int i = 0; i < numberOfPops; i++) { numberStack.Pop(); } if (numberStack.Contains(numberToFind)) { Console.WriteLine("true"); } else { Console.WriteLine(numberStack.Count > 0 ? numberStack.Min() : 0); } }
static void Main(string[] args) { Customer c1 = new Customer() { Id = 101, Name = "Oscar", Gender = "Male" }; Customer c2 = new Customer() { Id = 102, Name = "Alejandara", Gender = "Female" }; Customer c3 = new Customer() { Id = 103, Name = "Juan", Gender = "Male" }; Customer c4 = new Customer() { Id = 104, Name = "Johanna", Gender = "Female" }; Customer c5 = new Customer() { Id = 105, Name = "Ricardo", Gender = "Male" }; // LIFO -> Last in First Out Stack<Customer> stackCustomers = new Stack<Customer>(); stackCustomers.Push(c1); stackCustomers.Push(c2); stackCustomers.Push(c3); stackCustomers.Push(c4); stackCustomers.Push(c5); Customer tmpC1 = stackCustomers.Pop(); Console.WriteLine(tmpC1.Id + " - " + tmpC1.Name + " was removed!"); Console.WriteLine("Customers on the stack {0}", stackCustomers.Count); Console.ReadKey(); }
static void Main(string[] args) { Stack<int> numbers = new Stack<int>(); Console.ReadLine() .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList() .ForEach(n => numbers.Push(n)); while (numbers.Count > 0) { Console.Write($"{numbers.Pop()} "); } Console.WriteLine(); }
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; } } }