Пример #1
0
        static void Main(string[] args)
        {
            LinkedQueue <int> arrayQueue = new LinkedQueue <int>(3);

            arrayQueue.Enqueue(5);
            arrayQueue.Enqueue(6);
            arrayQueue.Enqueue(7);
            arrayQueue.Enqueue(8);
            arrayQueue.Enqueue(5);
            while (arrayQueue.Count != 0)
            {
                Console.WriteLine(arrayQueue.Dequeue());
            }
        }
Пример #2
0
        static void Main()
        {
            LinkedQueue <int> que = new LinkedQueue <int>();

            que.Enqueue(9);
            que.Enqueue(16);
            que.Enqueue(20);
            que.Enqueue(32);
            Console.WriteLine("Print original Queue:");
            que.Print();
            Console.WriteLine();
            Console.WriteLine("After calling Queue.Reverse() method:");
            que.ReverseQueue();
            que.Print();
        }
Пример #3
0
        static void Main()
        {
            LinkedQueue <int> que = new LinkedQueue <int>();

            que.Enqueue(14);
            que.Enqueue(32);
            que.Enqueue(16);
            que.Enqueue(12);
            que.Print();
            Console.WriteLine(que.Peek());
            que.Dequeue();
            que.Print();
            que.Dequeue();
            que.Print();
        }
Пример #4
0
        static void Main()
        {
            LinkedQueue <int> Q = new LinkedQueue <int>();

            // Loop to enqueue nums...
            for (int i = 1; i < 7; i++)
            {
                Q.Enqueue(i);
            }

            Console.WriteLine("Print Original Queue:");
            Q.Print();

            Console.WriteLine(); // Newline.


            // This method below is the where the magic happens...go to definition below.
            // Reverses Queue using stack...
            Q.ReverseQueue();

            Console.WriteLine("Print Reversed Queue:");

            Q.Print();

            // The Upshot: The ReverseQueue() method runs in O(n) time with O(n) space required.
            // Really it's O(2n) time -> O(n), so still linear time.
            // I modularized the Reverse() method into two separate functions using generics.
        }
Пример #5
0
        static void Main()
        {
            LinkedQueue <int> que = new LinkedQueue <int>();

            que.Enqueue(9);
            que.Enqueue(16);
            que.Enqueue(20);
            que.Enqueue(32);
            Console.WriteLine("Print original Queue:");
            que.Print();

            Console.WriteLine();

            Console.WriteLine("After calling Queue.Reverse() method:");
            que.ReverseQueue();
            que.Print();

            // The Upshot: The Queue.Reverse() method runs in O(n) time with O(n) space required.
            // Each of the two functions is O(n) time -> O(2n) for both, so still linear time.
            // I modularized the Reverse() code into two separate functions.
        }
Пример #6
0
        /// <summary>
        /// Reverses the queue. Calls Helper method.
        /// Pops item from returned stack and Enqueus them in reverse order.
        /// </summary>
        /// <returns>Reversed queue.</returns>
        public LinkedQueue <T> ReverseQueue()
        {
            if (IsEmpty())
            {
                throw new InvalidOperationException("Queue is empty.");
            }

            LinkedQueue <T> Q = this;

            var tempStack = QueueToStack(Q);

            while (!tempStack.IsEmpty())
            {
                T popped = tempStack.Peek();
                tempStack.Pop();
                Q.Enqueue(popped);
            }
            return(Q);
        }