Exemple #1
0
        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());
            }
        }
Exemple #2
0
 private void RearangeStacks()
 {
     while (_firstStack.Count != 0)
     {
         _secondStack.Push(_firstStack.Pop());
     }
 }
Exemple #3
0
        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]);
        }
Exemple #4
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>();
        }
Exemple #5
0
        public T Dequeue()
        {
            if (Count == 0)
            {
                throw new InvalidOperationException();
            }
            if (_secondStack.Count == 0)
            {
                RearangeStacks();
            }

            return(_secondStack.Pop());
        }