Exemplo n.º 1
0
        public static Stack reverse_if_backwards(Stack input)
        {
            Stack copy = (Stack)input.Clone();
            int? first;
            int? last;
            int? runner;

            Stack temp = new Stack();

            runner = input.pop();
            last = runner;
            while (runner != null)
            {
                if (runner != null) { temp.push((int)runner); }
                runner = input.pop();
            }

            first = temp.peek();

            //Console.WriteLine("First: " + first + " - Last: " + last);
            if (first > last)
            {
                // smallest on top
                return temp;
            }
            else
            {
                // largest on top
                return copy;
            }
        }
Exemplo n.º 2
0
        public static void printStack(Stack t)
        {
            Stack copy = (Stack)t.Clone();

            int levels = 0;
            int? item = copy.pop();

            Console.WriteLine("---");

            while (item != null)
            {
                levels++;
                Console.WriteLine("Stack level " + levels + ": " + item);
                item = copy.pop();
            }

            Console.WriteLine("---");

            Console.WriteLine("\n");
        }