public override T Dequeue() { if (IsEmpty()) { throw new QueueUnderFlowException(); } size--; if (stack1.IsEmpty() && shouldTransfer) { transferContents(stack2, stack1); return(stack1.Pop()); } else if (stack2.IsEmpty() && shouldTransfer) { transferContents(stack1, stack2); return(stack2.Pop()); } else if (!stack1.IsEmpty()) { return(stack1.Pop()); } else { return(stack2.Pop()); } }
public void ResizingArrayStackTest() { var stack = new ResizingArrayStack <int>(); stack.Push(1); stack.Push(2); Assert.IsTrue(stack.ItemCount == 2); Assert.IsTrue(stack.IsEmpty() == false); Assert.IsTrue(stack.Peek() == 2); Assert.IsTrue(stack.Pop() == 2); Assert.IsTrue(stack.ItemCount == 1); stack.Pop(); Assert.IsTrue(stack.IsEmpty()); for (int i = 0; i < 1000; i++) { stack.Push(i); } Assert.IsTrue(stack.ItemCount == 1000); for (int i = 0; i < 1000; i++) { stack.Pop(); } Assert.IsTrue(stack.ItemCount == 0); }
public void ResizingArrayStack_Positive() { var stack = new ResizingArrayStack <int>(); Assert.IsTrue(stack.IsEmpty()); stack.Push(1); stack.Push(3); stack.Push(5); stack.Push(7); stack.Push(9); Assert.AreEqual(5, stack.Size()); Assert.AreEqual(9, stack.Peek()); Assert.AreEqual(9, stack.Pop()); Assert.AreEqual(7, stack.Pop()); Assert.AreEqual(3, stack.Size()); Assert.AreEqual(5, stack.Peek()); stack.Push(66); var list = new List <int> { 66, 5, 3, 1 }; int index = 0; foreach (var i in stack) { Assert.AreEqual(list[index++], i); } }
public T Dequeue() { if (_outbox.IsEmpty()) { while (!_inbox.IsEmpty()) { _outbox.Push(_inbox.Pop()); } } return(_outbox.Pop()); }
private void transferContents(ResizingArrayStack <T> srcStack, ResizingArrayStack <T> destStack) { int size = srcStack.Size(); for (int i = 0; i < size; i++) { destStack.Push(srcStack.Pop()); } shouldTransfer = false; }
private bool MatchParens(string input) { var parens = new ResizingArrayStack <char>(1); foreach (var i in input) { switch (i) { case '[': case '(': case '{': parens.Push(i); break; case ']': if (parens.IsEmpty() || parens.Pop() != '[') { return(false); } break; case ')': if (parens.IsEmpty() || parens.Pop() != '(') { return(false); } break; case '}': if (parens.IsEmpty() || parens.Pop() != '{') { return(false); } break; default: break; } } return(true); }
public void ResizingArrayStackTest2() { int[] input = { 3, 2, 4, 6, 9 }; ResizingArrayStack <int> stack = new ResizingArrayStack <int>(); stack.Push(3); Assert.AreEqual("3 ", IteratorToString(stack)); stack.Push(4); Assert.AreEqual("4 3 ", IteratorToString(stack)); Assert.AreEqual(4, stack.Pop()); Assert.AreEqual(3, stack.Pop()); Assert.AreEqual(0, stack.Count, "New stack has zero size"); foreach (int i in input) { stack.Push(i); } Assert.AreEqual(input.Length, stack.Count); Assert.AreEqual("9 6 4 2 3 ", IteratorToString(stack)); }
public double Evaluate(string expression) { ResizingArrayStack <char> ops = new ResizingArrayStack <char>(); ResizingArrayStack <double> vals = new ResizingArrayStack <double>(); for (int i = 0; i < expression.Length; i++) { char currChar = expression[i]; if (currChar == '(') { ; } else if (currChar == '+') { ops.Push(expression[i]); } else if (currChar == '*') { ops.Push(expression[i]); } else if (currChar == ')') { char op = ops.Pop(); if (op == '+') { vals.Push(vals.Pop() + vals.Pop()); } else if (op == '*') { vals.Push(vals.Pop() * vals.Pop()); } } else { vals.Push(Char.GetNumericValue(currChar)); } } return(vals.Pop()); }
public void PushAndPopTest_ReferenceType() { IStack <String> stack = new ResizingArrayStack <String>(); stack.Push("1"); Assert.IsFalse(stack.IsEmpty()); Assert.AreEqual("1", stack.Peek()); Assert.AreEqual(1, stack.Size()); String value = stack.Pop(); Assert.IsTrue(stack.IsEmpty()); Assert.AreEqual(0, stack.Size()); Assert.AreEqual("1", value); }
public void PushAndPopTest_ValueType() { IStack <int> stack = new ResizingArrayStack <int>(); stack.Push(1); Assert.IsFalse(stack.IsEmpty()); Assert.AreEqual(1, stack.Peek()); Assert.AreEqual(1, stack.Size()); int value = stack.Pop(); Assert.IsTrue(stack.IsEmpty()); Assert.AreEqual(0, stack.Size()); Assert.AreEqual(1, value); }
public void ManyPopTest() { IStack <int> stack = new ResizingArrayStack <int>(); for (int i = 0; i < 10; i++) { stack.Push(i); } for (int i = 0; i < 10; i++) { int value = stack.Pop(); Assert.AreEqual(10 - i - 1, stack.Size()); Assert.AreEqual(10 - i - 1, value); } }
public IEnumerable <int> PathTo(int v) { if (HasPathTo(v)) { var path = new ResizingArrayStack <int>(); for (int i = v; i != _s; i = _pathTo[i]) { path.Push(i); } path.Push(_s); while (!path.IsEmpty()) { yield return(path.Pop()); } } ; }
void StackTest() { var strArr = FileHandler.ReadFileAsStrArr("words3.txt"); strArr.Show(); var stack = new ResizingArrayStack <string>(); //var stack = new NodeStack<string>(); foreach (var item in strArr) { stack.Push(item); } foreach (var item in stack) { Console.Write(item + " "); } Console.WriteLine(); while (!stack.IsEmpty()) { Console.Write(stack.Pop() + " "); } Console.ReadKey(); }
public void PopTest_EmptyStack() { IStack <int> stack = new ResizingArrayStack <int>(); stack.Pop(); }
private bool hasMatchingOpeningParenthesis(char openParen, ResizingArrayStack <char> stack) { return(!stack.IsEmpty() && stack.Pop() == parenthesesPair[openParen]); }