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. }
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(); }
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. }