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. }
/// <summary> /// Takes input queue and pushes items onto stack. /// </summary> /// <returns>The stack with queue items.</returns> /// <param name="q">Original queue.</param> public LinkedStack <T> QueueToStack(LinkedQueue <T> q) { LinkedStack <T> s = new LinkedStack <T>(); Node current = q.First; while (current.Next != null) { Node removed = current; s.Push(removed.Value); q.Dequeue(); current = current.Next; } return(s); }
/// <summary> /// Takes input queue and pushes items onto stack. /// </summary> /// <returns> The stack containing queue items.</returns> /// <param name="Q">Original queue.</param> public LinkedStack <T> QueueToStack(LinkedQueue <T> Q) { LinkedStack <T> S = new LinkedStack <T>(); Node current = Q.First; while (current.Next != null) { Node removed = current; S.Push(removed.Value); Q.Dequeue(); current = current.Next; } return(S); }
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()); } }
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(14); que.Enqueue(32); que.Enqueue(16); que.Enqueue(12); que.Print(); Console.WriteLine(que.Peek()); que.Dequeue(); que.Print(); que.Dequeue(); que.Print(); }
/// <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); }
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. }