Beispiel #1
0
        private static void TestMyQueue()
        {
            MyQueue <int> queue = new MyQueue <int>();

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

            // test enqueue
            queue.Enqueue(5);
            Console.WriteLine(queue.Size() == 1);
            queue.Enqueue(2);
            Console.WriteLine(queue.Size() == 2);
            queue.Enqueue(9);
            Console.WriteLine(queue.Size() == 3);
            // duplicate items not ignored
            queue.Enqueue(3);
            Console.WriteLine(queue.Size() == 4);
            queue.Enqueue(5);
            Console.WriteLine(queue.Size() == 5);

            // test dequeue
            int[] expected = { 5, 2, 9, 3, 5 };
            for (int i = 0, size = queue.Size(); i < size; i++)
            {
                Console.WriteLine(queue.Dequeue().Equals(expected[i]));
            }
        }
        public void Push(T item)
        {
            MyQueue <T> temp = new MyQueue <T>();

            temp.Enqueue(item);
            for (int i = 0, numItems = queue.Size(); i < numItems; i++)
            {
                temp.Enqueue(queue.Dequeue());
            }
            queue = temp;
        }