/// <summary> /// Helper method /// </summary> private void AlternateNodes() { int depth = Count; bool even = Count % 2 == 0; Stack tempStack = new Stack(); for (int i = 0; i < depth; i++) { if (even) { tempStack.Push(StackTwo.Pop()); } else { tempStack.Push(StackOne.Pop()); } } for (int j = 0; j < depth; j++) { if (even) { StackOne.Push(tempStack.Pop()); } else { StackTwo.Push(tempStack.Pop()); } } }
/// <summary> /// Method finds the beginning of the "Queue" or /// bottom of StackOne /// </summary> /// <returns></returns> public Node Peek() { while (StackOne.Peek() != null) { StackTwo.Push(StackOne.Pop()); } Front = StackTwo.Top; while (StackTwo.Peek() != null) { StackOne.Push(StackTwo.Pop()); } return(Front); }
/// <summary> /// Dequeues a node from the queue and returns its value. /// </summary> /// <returns> /// string: the string value of the dequeued Node /// </returns> public string Dequeue() { try { string returnValue = StackOneLastPopped ? StackTwo.Pop() : StackOne.Pop(); StackOneLastPopped = !StackOneLastPopped; Count--; return(returnValue); } catch (NullReferenceException e) { throw e; } }
/// <summary> /// Method uses the Stack Push and Pop methods to emulate FIFO. /// It will move all values from StackOne to StackTwo, pop and store the top /// from StackTwo and then proceed to re-fill StackOne to ensure Queue-Like /// Order /// </summary> /// <returns>Node that is removed</returns> public Node Dequeue() { while (StackOne.Peek() != null) { StackTwo.Push(StackOne.Pop()); } Node popped = StackTwo.Pop(); Front = StackTwo.Top; while (StackTwo.Peek() != null) { StackOne.Push(StackTwo.Pop()); } return(popped); }
/// <summary> /// Dequeue method /// </summary> /// <returns>string of returned node</returns> public string Dequeue() { try { if (Count % 2 == 0) { return(StackOne.Pop()); } else { return(StackTwo.Pop()); } } catch (NullReferenceException e) { throw new Exception(""); } }