/// <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()); } } }
public void PeekTest() { StackTwo s = new StackTwo(); int expected = 5; // TODO: Initialize to an appropriate value int actual; s.Push(0, expected); actual = s.Peek(0); Assert.AreEqual(expected, actual); }
public void EmptyStackTest() { bool expected = true; StackTwo s = new StackTwo(); for (int i = 0; i < s.NumberOfElements(); i++) { bool actual = s.IsEmpty(i); Assert.AreEqual(expected, actual); } }
/// <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> /// Enqueue method /// </summary> /// <param name="value">The value of which we are trying to "enqueue"</param> public void Enqueue(string value) { AlternateNodes(); if (Count % 2 != 0) { StackOne.Push(value); } else { StackTwo.Push(value); } Count++; }
public void PushTestOutofNumber() { try { StackTwo s = new StackTwo(); s.Push(0, 1); s.Push(0, 2); s.Push(0, 3); s.Push(0, 4); } catch (Exception e) { Assert.AreEqual("Out of space.", e.Message); } }
/// <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(""); } }