Exemple #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]));
            }
        }
Exemple #2
0
 /* Destructively transfer stack1 items to stack2 in reverse order */
 private void FlipStack(MyPQStack <T> stack1, MyPQStack <T> stack2)
 {
     for (int i = 0, numItems = stack1.Size(); i < numItems; i++)
     {
         stack2.Push(stack1.Pop());
     }
 }
Exemple #3
0
        public T Peek()
        {
            MyPQStack <T> temp = new MyPQStack <T>();

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

            FlipStack(temp, stack);
            return(peeked);
        }
Exemple #4
0
        public T Dequeue()
        {
            MyPQStack <T> temp = new MyPQStack <T>();

            FlipStack(stack, temp);
            T dequeued = temp.Pop();

            FlipStack(temp, stack);
            size--;
            return(dequeued);
        }
Exemple #5
0
 public void Clear()
 {
     stack = new MyPQStack <T>();
     size  = 0;
 }