Beispiel #1
0
        /// <summary>
        /// Sorts a stack and stores sorted one in another temp stack
        /// </summary>
        public void Sort()
        {
            while (!s1.IsEmpty())
            {
                int d = (int)s1.Pop();

                if (s2.IsEmpty())
                {
                    s2.Push(d);
                }
                else
                {
                    while (!s2.IsEmpty() && (int)s2.Peek() > d)
                    {
                        s1.Push(s2.Pop());
                    }

                    s2.Push(d);
                }
            }

            Console.WriteLine("Sorted Stack: ");

            while (!s2.IsEmpty())
            {
                Console.WriteLine(s2.Pop());
            }
        }
Beispiel #2
0
        static void Main()
        {
            LinkedListStack stack = new LinkedListStack();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            stack.Push(4);
            stack.Push(5);

            Console.WriteLine("Stack Pop: {0}", stack.Pop());
            Console.WriteLine("Stack Pop: {0}", stack.Pop());
            Console.WriteLine("Stack Pop: {0}", stack.Pop());
            Console.WriteLine("Stack Peek: {0}", stack.Peek());
            Console.WriteLine("Stack Pop: {0}", stack.Pop());

            stack.Push(6);
            Console.WriteLine("Stack Pop: {0}", stack.Pop());

            Console.ReadLine();
        }