Esempio n. 1
0
        private static void TestMyPQStack()
        {
            MyPQStack <int> stack = new MyPQStack <int>();

            // test empty stack
            Console.WriteLine(stack.Size() == 0);

            // test push and peek
            stack.Push(5);
            Console.WriteLine(stack.Size() == 1);
            Console.WriteLine(stack.Peek() == 5);
            stack.Push(2);
            Console.WriteLine(stack.Size() == 2);
            Console.WriteLine(stack.Peek() == 2);
            stack.Push(9);
            Console.WriteLine(stack.Size() == 3);
            Console.WriteLine(stack.Peek() == 9);
            // duplicate items not ignored
            stack.Push(3);
            Console.WriteLine(stack.Size() == 4);
            Console.WriteLine(stack.Peek() == 3);
            stack.Push(5);
            Console.WriteLine(stack.Size() == 5);
            Console.WriteLine(stack.Peek() == 5);

            // test pop
            int[] expected = { 5, 3, 9, 2, 5 };
            for (int i = 0, size = stack.Size(); i < size; i++)
            {
                Console.WriteLine(stack.Pop().Equals(expected[i]));
            }
        }
Esempio n. 2
0
        public T Peek()
        {
            MyPQStack <T> temp = new MyPQStack <T>();

            FlipStack(stack, temp);
            T peeked = temp.Peek();

            FlipStack(temp, stack);
            return(peeked);
        }