Esempio n. 1
0
        public void TestProg()
        {
            var max = 0;
            StackUsingLL <int> obj = new StackUsingLL <int>();

            string s = "1 94";
            string t = "3";
            string u = "0";

            for (int i = 0; i < 3; i++)
            {
                if (s.Length > 1)
                {
                    var c = s.Split(' ');
                    obj.Push(Convert.ToInt32(c[1]));
                    s = "";
                }
                else
                {
                    if (Convert.ToInt16(u) == 2)
                    {
                        obj.Pop();
                        t = "0";
                    }
                    if (Convert.ToInt16(t) == 3)
                    {
                        max = obj.MaxStack();
                        u   = "2";
                    }
                }
            }

            Assert.Equal(94, max);
        }
Esempio n. 2
0
        /// <summary>
        /// Method to convert an Infix expr to Prefix (Polish Notation). This method uses 2 stacks to hold the values.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ConvertInfixToPrefix(string input)
        {
            StackUsingLL <int> staging      = new StackUsingLL <int>();
            StackUsingLL <int> result       = new StackUsingLL <int>();
            StringBuilder      resultString = new StringBuilder();

            for (int i = input.Length - 1; i >= 0; i--)
            {
                if (input[i] == ')')
                {
                    staging.Push(input[i]);
                }
                else if (IsOperand(input[i]))
                {
                    result.Push(input[i]);
                }

                else if (input[i] == '(')
                {
                    while (staging.PeekTop() != ')')
                    {
                        result.Push(staging.Pop());
                    }
                    staging.Pop();
                }

                else
                {
                    if (!staging.IsEmpty() && CheckPrecedence(input[i]) >= CheckPrecedence(staging.PeekTop()))
                    {
                        staging.Push(input[i]);
                    }
                    else if (!staging.IsEmpty() && CheckPrecedence(input[i]) < CheckPrecedence(staging.PeekTop()))
                    {
                        result.Push(staging.Pop());
                        staging.Push(input[i]);
                    }
                    else if (staging.IsEmpty())
                    {
                        staging.Push(input[i]);
                    }
                }
            }

            while (!staging.IsEmpty())
            {
                result.Push(staging.Pop());
            }

            while (!result.IsEmpty())
            {
                resultString.Append(Convert.ToChar(result.Pop()));
            }

            return(resultString.ToString());
        }
Esempio n. 3
0
        public string ConvertInfixToPostfix(string infix)
        {
            StackUsingLL <int> staging = new StackUsingLL <int>();
            StringBuilder      result  = new StringBuilder();

            for (int i = 0; i < infix.Length; i++)
            {
                //If scanned character is an operand
                if (IsOperand(infix[i]))
                {
                    result.Append(infix[i]);
                }

                //If scanned character is '(', push it to stack
                else if (infix[i] == '(')
                {
                    staging.Push('(');
                }

                //If scanned character is ')', pop all  operators from stack until '(' is found
                else if (infix[i] == ')')
                {
                    while (!staging.IsEmpty() && staging.PeekTop() != '(')
                    {
                        result.Append(Convert.ToChar(staging.Pop()));
                    }
                    //Invalid string check
                    if (!staging.IsEmpty() && staging.PeekTop() != '(')
                    {
                        return(null);
                    }
                    else
                    {
                        staging.Pop();
                    }
                }

                //If operator
                else
                {
                    while (!staging.IsEmpty() && (CheckPrecedence(infix[i]) <= CheckPrecedence(Convert.ToChar(staging.PeekTop()))))
                    {
                        result.Append(Convert.ToChar(staging.Pop()));
                    }
                    staging.Push(infix[i]);
                }
            }

            while (!staging.IsEmpty())
            {
                result.Append(Convert.ToChar(staging.Pop()));
            }

            return(result.ToString());
        }
Esempio n. 4
0
        public int EqualStacks_Method(int[] h1, int[] h2, int[] h3)
        {
            StackUsingLL <int> obj1 = new StackUsingLL <int>();

            for (int i = h1.Length - 1; i >= 0; i--)
            {
                obj1.Push(h1[i]);
            }

            StackUsingLL <int> obj2 = new StackUsingLL <int>();

            for (int i = h2.Length - 1; i >= 0; i--)
            {
                obj2.Push(h2[i]);
            }

            StackUsingLL <int> obj3 = new StackUsingLL <int>();

            for (int i = h3.Length - 1; i >= 0; i--)
            {
                obj3.Push(h3[i]);
            }

            while
            (
                obj1.SumStack() != obj2.SumStack() ||
                obj2.SumStack() != obj3.SumStack() ||
                obj1.SumStack() != obj3.SumStack()
            )
            {
                var c = Math.Max(obj1.SumStack(), Math.Max(obj2.SumStack(), obj3.SumStack()));

                if (c == obj1.SumStack())
                {
                    obj1.Pop();
                }

                else if (c == obj2.SumStack())
                {
                    obj2.Pop();
                }
                else if (c == obj3.SumStack())
                {
                    obj3.Pop();
                }
            }

            return(obj1.SumStack());
        }
Esempio n. 5
0
        static int TwoStacks(int x, int[] a, int[] b)
        {
            var obj1     = new StackUsingLL <int>();
            var sumStack = new StackUsingLL <int>();

            for (int i = a.Length - 1; i >= 0; i--)
            {
                obj1.Push(a[i]);
            }

            var obj2 = new StackUsingLL <int>();

            for (int i = b.Length - 1; i >= 0; i--)
            {
                obj2.Push(b[i]);
            }

            while (sumStack.SumStack() + obj1.PeekTop() <= x)
            {
                sumStack.Push(obj1.Pop());
            }


            int countStack1 = sumStack.StackLength();
            int sum         = sumStack.SumStack();
            int max         = countStack1;
            int countStack2 = 0;

            while (obj2.StackLength() != 0 && sumStack.StackLength() > 0)
            {
                sum = sum + obj2.Pop();
                countStack2++;
                if (sum > x)
                {
                    sum = sum - sumStack.Pop();
                    countStack1--;
                }
                if (sum <= x && countStack1 + countStack2 > max)
                {
                    max = countStack1 + countStack2;
                }
            }
            return(max);
        }
Esempio n. 6
0
        public void StackLLPushPop_Test()
        {
            var obj = new StackUsingLL <int>();

            obj.Push(10);
            obj.Push(11);
            obj.Push(12);
            obj.Push(13);
            obj.Push(14);
            obj.Push(15);
            obj.Push(5);
            obj.Push(4);
            obj.Push(3);
            obj.Push(1);
            Assert.Equal(1, obj.Pop());
            Assert.Equal(3, obj.Pop());
            Assert.Equal(4, obj.Pop());
            Assert.Equal(5, obj.Pop());
        }
Esempio n. 7
0
        public void StackLL_Test()
        {
            var obj = new StackUsingLL <int>();

            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(5);
            obj.Push(4);
            obj.Push(3);
            obj.Push(1);
            obj.EmptyStack();
            Assert.Equal(0, obj.StackLength());
            obj.Push(2);
            Assert.Equal(1, obj.StackLength());
        }
Esempio n. 8
0
        public string IsBalanced(string s)
        {
            StackUsingLL <int> obj = new StackUsingLL <int>();

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '{' || s[i] == '[' || s[i] == '(')
                {
                    obj.Push(s[i]);
                }
                else
                {
                    if (obj.StackLength() > 0)
                    {
                        switch (s[i])
                        {
                        case '}': if (obj.PeekTop() == '{')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;


                        case ']':  if (obj.PeekTop() == '[')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;

                        case ')':   if (obj.PeekTop() == '(')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;
                        }
                    }
                    else
                    {
                        return("NO");
                    }
                }
            }

            if (obj.StackLength() == 0)
            {
                return("YES");
            }
            else
            {
                return("NO");
            }
        }