Example #1
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.
        }
Example #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();
        }
Example #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();
        }
Example #4
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.
        }