public void PushPopRangeTest() { //arrange Stack1 stack = new Stack1(); int result; stack.Push(10); Assert.AreEqual(stack.Max(), 10, "Value should be 10"); stack.Push(15); Assert.AreEqual(stack.Max(), 15, "Value should be 15"); stack.Push(3); Assert.AreEqual(stack.Max(), 15, "Value should be 15"); stack.Push(100); Assert.AreEqual(stack.Max(), 100, "Value should be 100"); stack.Pop(); Assert.AreEqual(stack.Max(), 15, "Value should be 15"); stack.Pop(); Assert.AreEqual(stack.Max(), 15, "Value should be 15"); stack.Pop(); Assert.AreEqual(stack.Max(), 10, "Value should be 10"); }
public void TestMethod1() { Stack1 S = new Stack1(); S.Push("1"); S.Push("2"); S.Push("3"); Assert.AreEqual("3", S.Pop()); Assert.AreEqual("2", S.Pop()); }
public T DeQueue1() { if (Stack1 == null || Stack1.HeadStackNodeSingly == null) { return(default(T)); } return(Stack1.Pop()); }
public Node Dequeue() { while (Stack1.Top.Next != null) { Stack2.Push(Stack1.Pop()); } Node wantedNode = Stack1.Pop(); return(wantedNode); }
/// <summary> /// This method removes the node at the "front" of the queue; which is at the bottom of stack1. /// </summary> /// <returns>This is the node at the bottom of stack 1</returns> public Node Dequeue() { if (Stack2.Peek() == null) { while (Stack1.Peek().Next != null) { Stack2.Push(Stack1.Pop()); } Stack2.Push(Stack1.Pop()); } return(Stack2.Pop()); }
/// <summary> /// while theres more than one node in Stack1, pop them off into stack2, then return /// the last node in stack1, then put all the stack2 nodes back into stack1 /// </summary> /// <returns></returns> public Node Dequeue() { while (Stack1.Top != null) { Stack2.Push(Stack1.Pop()); if (Stack1.Top.Next == null) { Stack2.Push(Stack1.Pop()); } } return(Stack2.Pop()); }
/// <summary> /// Remove and return the "front" of the queue, which is actually the top of the primary stack. /// </summary> /// <returns>Front node in queue</returns> public string Dequeue() { if (Stack1.Top == null) { throw new Exception("Empty pseudoqueue"); } else { string result = Stack1.Pop(); Front = Stack1.Top; return(result); } }
public void TestMaxWhenWasAndPoped() { //arrange Stack1 stack = new Stack1(); int[] values = new int[] { 1, 2, 10, 5, 20 }; int expected = 10; int result; //act foreach (int val in values) { stack.Push(val); } stack.Pop(); stack.Pop(); result = stack.Max(); //assert Assert.AreEqual(result, expected); }
public static void Print() { while (Stack1.Count > 0) { var element = Stack1.Pop(); Stack2.Push(element); } Console.WriteLine("Element at the head of the queue: {0}", Stack2.Peek()); while (Stack2.Count > 0) { var element = Stack2.Pop(); Stack1.Push(element); } }
public T DeQueue2() { if (Stack2 == null) { Stack2 = new StackLinkedList <T>(); } while (Stack1.HeadStackNodeSingly != null) { Stack2.Push(new StackNodeSingly <T>(Stack1.Pop())); } return(Stack2.Pop()); }
public void TestPushPopWithOneValue() { //arrange Stack1 stack = new Stack1(); int val = 5; int expected = 5; int result; //act stack.Push(val); result = stack.Pop(); //assert Assert.AreEqual(result, expected); }
public static void Dequeue() { while (Stack1.Count > 1) { var element = Stack1.Pop(); Stack2.Push(element); } var dequed = Stack1.Pop(); Console.WriteLine("Dequed element: {0}", dequed); while (Stack2.Count > 0) { var element = Stack2.Pop(); Stack1.Push(element); } }
/// <summary> /// Mimic a Queue object's enqueue method with stacks, /// and returns the value what would be the front object /// </summary> /// <param name="value"> value to enqueue </param> /// <returns></returns> public T Enqueue(T value) { Stack1.Push(value); while (!Stack1.IsEmpty()) { Stack2.Push(Stack1.Pop()); } T frontValue = Stack2.Peek(); while (!Stack2.IsEmpty()) { Stack1.Push(Stack2.Pop()); } return(frontValue); }
/// <summary> /// emulates the "dequeue" method using 2 stacks /// </summary> /// <returns>the node that was "dequeued" from the stack</returns> public Node Dequeue() { if (Stack1.Top.Next == null) { return(Stack1.Pop()); } while (Stack1.Top.Next != null) { Stack2.Push(Stack1.Pop()); } Node tmpNode = Stack1.Pop(); while (Stack2.Top.Next != null) { Stack1.Push(Stack2.Pop()); } return(tmpNode); }
//Dequeue() returns the first value that was placed into stack1 public Node Dequeue() { //keep going through stack1 until we reach the bottom while (Stack1.Top.Next != null) { Stack2.Push(Stack1.Pop()); //this looping mechanism will only leave one node behind... } //which is this node, so it'll be popped off //Since this method returns a node, we init a Node that references it //so we can return this node. This is the node WE WANT Node returnNode = Stack1.Pop(); //this will put all the guts of stack1 back from stack2 while (Stack2.Top != null) { Stack1.Push(Stack2.Pop()); } return(returnNode); }
/// <summary> /// Add a new item to the "rear" of the queue, which is actually the bottom of the primary stack /// </summary> /// <param name="val">Value to be enqueued</param> public void Enqueue(string val) { if (Stack1.Top == null) { Stack1.Push(val); Front = Stack1.Top; } else { while (Stack1.Top != null) { Stack2.Push(Stack1.Pop()); } Stack1.Push(val); while (Stack2.Top != null) { Stack1.Push(Stack2.Pop()); } Front = Stack1.Top; } }
/// <summary> /// Removes a node from the queue /// </summary> /// <returns>returns the node that was dequeued</returns> public Node Dequeue() { if (Stack2.Peek() == null) { return(null); } while (Stack2.Peek().Next != null) { Stack1.Push(Stack2.Pop()); } Node temp = Stack2.Pop(); while (Stack1.Peek() != null) { Stack2.Push(Stack1.Pop()); } return(temp); }
/// <summary> /// Mimic a Queue object's dequeue method with stacks. /// Pop() what would be the front node and return the value /// </summary> /// <returns></returns> public T Dequeue() { try { while (!Stack1.IsEmpty()) { Stack2.Push(Stack1.Pop()); } T frontValue = Stack2.Pop(); while (!Stack2.IsEmpty()) { Stack1.Push(Stack2.Pop()); } return(frontValue); } catch (NullReferenceException e) { throw new NullReferenceException($"Your stack is empty, womp womp\n{e}"); } }
public void EnQueue1(T data) { if (Stack1 == null) { Stack1 = new StackLinkedList <T>(); } if (Stack2 == null) { Stack2 = new StackLinkedList <T>(); } var newData = new StackNodeSingly <T>(data); while (Stack1.HeadStackNodeSingly != null) { Stack2.Push(new StackNodeSingly <T>(Stack1.Pop())); } Stack1.Push(newData); while (Stack2.HeadStackNodeSingly != null) { Stack1.Push(new StackNodeSingly <T>(Stack2.Pop())); } }