Exemplo n.º 1
0
 public void StackWithLinkedList_Pop_should_throw_exception_when_stack_is_empty()
 {
     try
     {
         st.Pop();
         Assert.Fail();
     }
     catch (InvalidOperationException ex)
     {
         Assert.AreEqual("The stack is empty", ex.Message);
     }
     catch (Exception)
     {
         Assert.Fail();
     }
 }
Exemplo n.º 2
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.º 3
0
        public void Pop_Two_Elements_In_A_Stack_With_Three_Elements()
        {
            var stack = new StackWithLinkedList <int>();

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

            var item1 = stack.Pop();
            var item2 = stack.Pop();

            Assert.Equal(30, item1);
            Assert.Equal(20, item2);
            Assert.Equal(1, stack.Length);
            Assert.Equal(10, stack.Top.Value);
            Assert.Equal(10, stack.Bottom.Value);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void Pop_An_Element_In_A_Stack_With_No_Element()
        {
            var stack = new StackWithLinkedList <int>();

            var item = stack.Pop();

            Assert.Equal(0, item);
            Assert.Equal(0, stack.Length);
            Assert.Null(stack.Top);
            Assert.Null(stack.Bottom);
        }
Exemplo n.º 6
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 Pop_WhenStackIsEmpty_ShouldThrowInvalidOperationException()
 {
     // Arrange & Act & Assert
     Assert.Throws <InvalidOperationException>(() => _stack.Pop());
 }