public void Push(T item) { while (_firstStack.Count > 0 && IsLessThan(_firstStack.Peek(), item)) { _secondStack.Push(_firstStack.Pop()); } _firstStack.Push(item); while (_secondStack.Count > 0) { _firstStack.Push(_secondStack.Pop()); } }
private void RearangeStacks() { while (_firstStack.Count != 0) { _secondStack.Push(_firstStack.Pop()); } }
public void Should_Push_And_Pop_Correct_Order() { //arrange var data = new int[] { 1, 2, 3 }; var stack = new MyLinkedListStack <int>(); //act stack.Push(data[0]); stack.Push(data[1]); stack.Push(data[2]); var first = stack.Pop(); var second = stack.Pop(); var third = stack.Pop(); //assert first.ShouldBeEquivalentTo(data[2]); second.ShouldBeEquivalentTo(data[1]); third.ShouldBeEquivalentTo(data[0]); }
public void Should_Pop_Throw_If_Stack_Is_Empty() { //arrange var stack = new MyLinkedListStack <int>(); //act Action act = () => stack.Pop(); //assert act.ShouldThrow <InvalidOperationException>(); }
public T Dequeue() { if (Count == 0) { throw new InvalidOperationException(); } if (_secondStack.Count == 0) { RearangeStacks(); } return(_secondStack.Pop()); }