Exemplo n.º 1
0
        public void StackWithLinkedList_IsFull_should_return_true_when_stack_has_capacity_number_of_elements()
        {
            StackWithLinkedList <int> st2 = new StackWithLinkedList <int>(2);

            st2.Push(1);
            st2.Push(3);

            Assert.AreEqual(true, st2.IsFull());
        }
Exemplo n.º 2
0
        public void Push_Two_Elements()
        {
            var stack = new StackWithLinkedList <int>();

            stack.Push(10);
            stack.Push(20);

            Assert.Equal(2, stack.Length);
            Assert.Equal(20, stack.Top.Value);
            Assert.Equal(10, stack.Bottom.Value);
        }
Exemplo n.º 3
0
        public void Pop_An_Element_In_A_Stack_With_Two_Elements()
        {
            var stack = new StackWithLinkedList <int>();

            stack.Push(10);
            stack.Push(20);

            var item = stack.Pop();

            Assert.Equal(20, item);
            Assert.Equal(1, stack.Length);
            Assert.Equal(10, stack.Top.Value);
            Assert.Equal(10, stack.Bottom.Value);
        }
Exemplo n.º 4
0
        private void Stack_Implementation_Of_Linkedlist_Click(object sender, EventArgs e)
        {
            StackWithLinkedList list = new StackWithLinkedList();

            Console.WriteLine("Checking Is Empty - " + list.IsEmpty());
            Console.WriteLine("Push 5 - "); list.Push(5);
            Console.WriteLine("Push 4 - "); list.Push(4);
            Console.WriteLine("Push 3 - "); list.Push(3);

            Console.WriteLine("Pop 3 - " + list.Pop().data);
            Console.WriteLine("Pop 4 - " + list.Pop().data);
            Console.WriteLine("Checking Is Empty - " + list.IsEmpty());
            Console.WriteLine("Pop 5 - " + list.Pop().data);

            Console.WriteLine("Checking Is Empty - " + list.IsEmpty());
        }
Exemplo n.º 5
0
        public void StackWithLinkedList_IsFull_should_return_false_when_stack_has_less_elements()
        {
            StackWithLinkedList <int> st2 = new StackWithLinkedList <int>(2);

            st2.Push(1);

            Assert.AreEqual(false, st2.IsFull());
        }
Exemplo n.º 6
0
        public void StackWithLinkedList_Push_should_throw_exception_when_stack_is_full()
        {
            StackWithLinkedList <int> st2 = new StackWithLinkedList <int>(2);

            st2.Push(1);
            st2.Push(3);
            try
            {
                st2.Push(4);
                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                Assert.AreEqual("The stack is full", ex.Message);
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
Exemplo n.º 7
0
        public void Stack_Is_Not_Empty()
        {
            var stack = new StackWithLinkedList <int>();

            stack.Push(10);
            var isEmpty = stack.IsEmpty();

            Assert.False(isEmpty);
            Assert.Equal(1, stack.Length);
            Assert.NotNull(stack.Top);
            Assert.NotNull(stack.Bottom);
        }
Exemplo n.º 8
0
        public void Pop_An_Element_In_A_Stack_With_One_Element()
        {
            var stack = new StackWithLinkedList <int>();

            stack.Push(20);

            var item = stack.Pop();

            Assert.Equal(20, item);
            Assert.Equal(0, stack.Length);
            Assert.Null(stack.Top);
            Assert.Null(stack.Bottom);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
        public void Size_WhenCalled_ShouldReturnNumberOfItems(int range)
        {
            // Arrange
            for (var i = 1; i <= range; i++)
            {
                _stack.Push(i);
            }

            // Act
            var result = _stack.Size;

            // Assert
            Assert.That(result, Is.EqualTo(range));
        }
Exemplo n.º 12
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());
            }
        }
Exemplo n.º 13
0
        public void StackWithLinkedList_IsEmpty_should_return_false_when_stack_is_not_empty()
        {
            st.Push(1);

            Assert.AreEqual(false, st.IsEmpty());
        }