Exemple #1
0
 public void StackWithLinkedList_Peek_should_throw_exception_when_stack_is_empty()
 {
     try
     {
         st.Peek();
         Assert.Fail();
     }
     catch (InvalidOperationException ex)
     {
         Assert.AreEqual("The stack is empty", ex.Message);
     }
     catch (Exception)
     {
         Assert.Fail();
     }
 }
Exemple #2
0
        public long largestRectangle(int[] h)
        {
            // first of all i define variable which type is stack.
            var _stack = new StackWithLinkedList <int>();

            var _maximum_area  = 0; //as a result that should return at the end of method
            var _lengthOfArray = h.Length;
            var _counter       = 0;

            // secondly start from first bar, and do following for every bar ‘h[i]’ where ‘_counter’ varies from 0 to _lengthOfArray-1.
            while (_counter < _lengthOfArray)
            {
                if (_stack.IsNull() || h[_stack.Peek()] <= h[_counter])
                {
                    _stack.Push(_counter++);
                }
                else if (h[_stack.Peek()] > h[_counter])
                {
                    //keep top of stack and remove it
                    var _topOfStack = _stack.Pop();
                    //calculate with top of stack
                    var _calculateArea = h[_topOfStack] * (_stack.IsNull() ? _counter : _counter - _stack.Peek() - 1);
                    if (_maximum_area < _calculateArea)
                    {
                        _maximum_area = _calculateArea;
                    }
                }
            }

            //finally if stack not is null should remove one by one from stack and calculate area with each one
            while (!_stack.IsNull())
            {
                //keep top of stack and remove it
                var _topOfStack = _stack.Pop();
                //calculate with top of stack
                var _calculateArea = h[_topOfStack] * (_stack.IsNull() ? _counter : _counter - _stack.Peek() - 1);
                if (_maximum_area < _calculateArea)
                {
                    _maximum_area = _calculateArea;
                }
            }

            return(_maximum_area);
        }
Exemple #3
0
        public void Peek_An_Element_In_A_Stack_With_No_Element()
        {
            var stack = new StackWithLinkedList <int>();

            var item = stack.Peek();

            Assert.Equal(0, item);
            Assert.Equal(0, stack.Length);
            Assert.Null(stack.Top);
            Assert.Null(stack.Bottom);
        }
Exemple #4
0
        public void Peek_Element_In_A_Stack_With_One_Element()
        {
            var stack = new StackWithLinkedList <int>();

            stack.Push(20);

            var item = stack.Peek();

            Assert.Equal(20, item);
            Assert.Equal(1, stack.Length);
            Assert.Equal(20, stack.Top.Value);
            Assert.Equal(20, stack.Bottom.Value);
        }
Exemple #5
0
        public override void Push(int value)
        {
            base.Push(value);

            var sorted = new StackWithLinkedList <int>();

            while (!base.IsEmpty())
            {
                int element = base.Pop();
                while (!sorted.IsEmpty() && sorted.Peek() > element)
                {
                    base.Push(sorted.Pop());
                }

                sorted.Push(element);
            }

            while (!sorted.IsEmpty())
            {
                base.Push(sorted.Pop());
            }
        }
 public void Peek_WhenStackIsEmpty_ShouldThrowInvalidOperationException()
 {
     // Arrange & Act & Assert
     Assert.Throws <InvalidOperationException>(() => _stack.Peek());
 }