public void Count()
        {
            StackUsingLinkedList <int> stack = new StackUsingLinkedList <int>();

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

            /*
             * 40    --- Top
             * 30
             * 20
             * 10
             */

            stack.Clear();

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


            /*
             * 30   --- Top
             * 20
             * 10
             */

            Assert.AreEqual(stack.Count(), 3);
        }
 public void StackUsingLinkedListThrowsException_WhenPopOnEmpty()
 {
     var st = new StackUsingLinkedList<int>();
     st.Push(1);
     var item = st.Pop();
     st.Pop();
 }
        public void VerifyPushInStack()
        {
            var actual = new StackUsingLinkedList();

            actual.Push(12);
            actual.Push(14);
            actual.Push(16);

            Assert.AreEqual(3, actual.Count());
        }
示例#4
0
        public static double EvaluatePostFixExpression(QueueUsingDoublyLinkedList <string> postfix)
        {
            StackUsingLinkedList <string> stack = new StackUsingLinkedList <string>();
            var    count  = postfix.Count;
            double answer = 0;

            for (int i = 0; i < count; i++)
            {
                if (stack.Count >= 2 && postfix.Peek().Length == 1 && IsOperator(postfix.Peek()[0]) == true)
                {
                    double x = 0;
                    double y = 0;
                    if (Double.TryParse(stack.Peek(), out x))
                    {
                        x = Convert.ToDouble(stack.Pop());
                    }
                    if (Double.TryParse(stack.Peek(), out y))
                    {
                        y = Convert.ToDouble(stack.Pop());
                    }
                    double result = Calculate(y, postfix.Dequeue()[0], x);
                    stack.Push(result.ToString());
                }

                else
                {
                    stack.Push(postfix.Dequeue());
                }
            }

            if (stack.Count != 0)
            {
                if (double.TryParse(stack.Peek(), out answer) == true)
                {
                    answer = Convert.ToDouble(stack.Pop());
                }

                else
                {
                    if (stack.Count != 1)
                    {
                        stack.Pop();
                    }
                    if (double.TryParse(stack.Peek(), out answer) == true)
                    {
                        answer = Convert.ToDouble(stack.Pop());
                    }
                }
            }

            return(answer);
        }
        public void VerifyPeekInStack()
        {
            var actual = new StackUsingLinkedList();

            actual.Push(12);
            actual.Push(14);
            actual.Push(16);

            Assert.AreEqual(3, actual.Count());

            var value = actual.Peek();

            Assert.AreEqual(16, value);
            Assert.AreEqual(3, actual.Count());
        }
        public void StackUsingLinkedList_SuccessTests()
        {
            var st = new StackUsingLinkedList<int>();

            st.Push(1);
            st.Push(2);
            st.Push(3);
            st.Push(4);
            st.Push(5);

            Assert.AreEqual(5, st.Pop());
            Assert.AreEqual(4, st.Pop());
            Assert.AreEqual(3, st.Pop());
            Assert.AreEqual(2, st.Pop());
            Assert.AreEqual(1, st.Pop());
        }
        public void Push()
        {
            StackUsingLinkedList <int> stack = new StackUsingLinkedList <int>();

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

            /*
             * 40    --- Top
             * 30
             * 20
             * 10
             */

            Assert.AreEqual(stack.Peek(), 40);
            Assert.AreEqual(stack.Count(), 4);
        }
示例#8
0
        public void DisplayStack()
        {
            Console.WriteLine("Select Question[1-12]");
            int choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                #region CSharp Stack Collection
            case 1:
                Console.WriteLine("--------" + choice + ".CSharp Stack Collection --------");
                StackCollectioninCsharp <int> sk1 = new StackCollectioninCsharp <int>();
                int[] arr1 = { 10, 78, 34, 65 };
                foreach (var v in arr1)
                {
                    sk1.Push(v);
                }
                sk1.Print();
                Console.WriteLine($"Count is :{sk1.myStack.Count}");
                Console.WriteLine($"78 Is Present :{sk1.myStack.Contains(78)}");
                sk1.myStack.Pop();
                sk1.Print();
                break;
                #endregion

                #region Stack using Array
            case 2:
                Console.WriteLine("--------" + choice + ".Stack using Array--------");
                StackUsingArray sk2 = new StackUsingArray(10);
                sk2.Push(10);
                sk2.Push(20);
                sk2.Push(30);
                sk2.Push(40);
                sk2.Print();
                sk2.Pop();
                sk2.Print();
                break;
                #endregion

                #region Stack Using Linked List
            case 3:
                Console.WriteLine("--------" + choice + ".Stack Using Linked List--------");
                StackUsingLinkedList <int> sk3 = new StackUsingLinkedList <int>();
                int[] arr3 = { 10, 20, 30, 40 };
                foreach (var v in arr3)
                {
                    sk3.Push(v);
                }
                sk3.Print();
                sk3.Peek();
                sk3.Print();
                sk3.Pop();
                sk3.Print();
                Console.WriteLine($"Size Of Stack: {sk3.GetSize()}");
                break;
                #endregion

                #region Balanced Expression
            case 4:
                Console.WriteLine("--------" + choice + ".Balanced Expression--------");
                BalancingSymbolPattern sk4 = new BalancingSymbolPattern();
                Console.WriteLine($"Expression is Balanced using Stack: {sk4.CheckPattern("[{(}]", "Stack")}");
                Console.WriteLine($"Expression is Balanced  using Stack Adt: {sk4.CheckPattern("[{)}]", "StackADT")}");
                Console.WriteLine($"Expression is Balanced using Stack: {sk4.CheckPattern("[{()}]", "Stack")}");
                Console.WriteLine($"Expression is Balanced  using Stack Adt: {sk4.CheckPattern("[{()}]", "StackADT")}");
                break;
                #endregion

                #region Postfix Expression Evaluation Using Stack
            case 5:
                Console.WriteLine("--------" + choice + ".Postfix Expression Evaluation  Using Stack--------");
                PostfixImplimentationUsingStack sk5 = new PostfixImplimentationUsingStack();
                Console.WriteLine($"Postfix Expression result is {sk5.EvaluatePostfixExpression("123*+5-")}");
                break;
                #endregion

                #region Infix to Postfix
            case 6:
                Console.WriteLine("--------" + choice + ".Infix to Postfix--------");
                InfixToPostFix sk6 = new InfixToPostFix();
                string         exp = "a+b*(c^d-e)^(f+g*h)-i";
                //string exp ="a*b-(c+d)-e";
                Console.WriteLine($"Infix {exp} of  Postfix is : {sk6.ConvertInfixToPostFix(exp.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Infix To Prefix
            case 7:
                Console.WriteLine("--------" + choice + ".Infix To Prefix--------");
                InfixToPrefix sk7 = new InfixToPrefix();
                //string exp7 = "a*b-(c+d)-e";
                string exp7 = "((a/b)+c)-(d+(e*f))";
                Console.WriteLine($"Infix {exp7} of Prefix is {sk7.ConvertInfixToPrefix(exp7.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Convert Prefix and Postfix to Infix
            case 8:
                Console.WriteLine("--------" + choice + ".Convert Prefix and Postfix to Infix--------");
                ConvertToInfix sk8     = new ConvertToInfix();
                string         exppre8 = "*-A/BC-/AKL";
                Console.WriteLine($"Pefix {exppre8} of Infix is {sk8.ConvertPreToInfix(exppre8.Replace(" ", string.Empty))}");
                string expost8 = " ab*c+";
                Console.WriteLine($"Postfix {expost8} of Infix is {sk8.ConvertPostToInfix(expost8.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Convert Post to Pre and Viceversa
            case 9:
                Console.WriteLine("--------" + choice + ".Convert Post to Pre and Viceversa--------");
                ConvertPreToPost_PostToPre sk9 = new ConvertPreToPost_PostToPre();
                string exp9 = "*-A/BC-/AKL";
                Console.WriteLine(sk9.ConvertExpression(exp9.Replace(" ", string.Empty)));
                break;
                #endregion

                #region Sort Stack using Recursion
            case 10:
                Console.WriteLine("--------" + choice + ".Sort Stack using Recursion--------");
                string    str10 = "-3 14 18 -5 30";
                SortStack sk10  = new SortStack(str10);
                sk10.PrintStack();
                break;
                #endregion

                #region Get Min From Stack with O(1)
            case 11:
                Console.WriteLine("--------" + choice + ". Get Min From Stack with O(1)--------");
                GetMinStack sk11 = new GetMinStack();
                sk11.Push(2);
                sk11.Push(4);
                sk11.Push(6);
                sk11.Push(1);
                sk11.Push(5);
                Console.WriteLine("Stack is 5,1,6,4,2");
                Console.WriteLine($"min value in stack is {sk11.GetMin()} and top of stack is {sk11.Top()}");
                sk11.Pop();
                sk11.Pop();
                Console.WriteLine("After 2 Pops");
                Console.WriteLine($"min value in stack is {sk11.GetMin()} and top of stack is {sk11.Top()}");
                break;
                #endregion

                #region Get MAX From Stack with O(1)
            case 12:
                Console.WriteLine("--------" + choice + ".Get MAX From Stack with O(1)--------");
                GetMaxStack sk12 = new GetMaxStack();
                sk12.Push(2);
                sk12.Push(7);
                sk12.Push(6);
                sk12.Push(8);
                sk12.Push(5);
                Console.WriteLine("Stack is 5,8,6,7,2");
                Console.WriteLine($"Max value in stack is {sk12.GetMax()} and top of stack is {sk12.GetTop()}");
                sk12.Pop();
                sk12.Pop();
                Console.WriteLine("After 2 Pops");
                Console.WriteLine($"Max value in stack is {sk12.GetMax()} and top of stack is {sk12.GetTop()}");
                break;
                #endregion

                #region
            case 13:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

                #region
            case 14:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

                #region
            case 15:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

            default:
                Console.WriteLine("Oops! Inavalid Choice.");
                break;
            }
        }
示例#9
0
        public static QueueUsingDoublyLinkedList <string> ConvertToPostfix(string exp)
        {
            var postfix = new QueueUsingDoublyLinkedList <string>();
            StackUsingLinkedList <char> stack = new StackUsingLinkedList <char>();
            StringBuilder num = new StringBuilder();

            for (int x = 0; x < exp.Length; x++)
            {
                if (Char.IsDigit(exp[x]) == true)
                {
                    if (x != exp.Length - 1 && (Char.IsDigit(exp[x + 1]) == true || exp[x + 1] == '.' || exp[x + 1] == ','))
                    {
                        num.Append(exp[x]);
                    }

                    else
                    {
                        num.Append(exp[x]);
                        postfix.Enqueue(num.ToString());
                        num.Clear();
                        if (x != exp.Length - 1 && exp[x + 1] == '(')
                        {
                            while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence('*', stack.Peek()) == true)
                            {
                                postfix.Enqueue(stack.Pop().ToString());
                            }
                            stack.Push('*');
                        }
                    }
                }

                else if (exp[x] == '.')
                {
                    num.Append(exp[x]);
                }

                else if (exp[x] == '-')
                {
                    if (x == 0 || IsOperator(exp[x - 1]) == true || exp[x - 1] == '(')
                    {
                        if (exp.Length != 1 && x != exp.Length - 1 && exp[x + 1] == '(')
                        {
                            exp = exp.Substring(0, x + 1) + '1' + exp.Substring(x + 1);
                        }

                        num.Append(exp[x]);
                    }

                    else
                    {
                        while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence(exp[x], stack.Peek()) == true)
                        {
                            postfix.Enqueue(stack.Pop().ToString());
                        }
                        stack.Push(exp[x]);
                    }
                }

                else if (exp[x] == '(')
                {
                    stack.Push(exp[x]);
                }

                else if (IsOperator(exp[x]) == true)
                {
                    while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence(exp[x], stack.Peek()) == true)
                    {
                        postfix.Enqueue(stack.Pop().ToString());
                    }
                    stack.Push(exp[x]);
                }

                else if (exp[x] == ')')
                {
                    while (stack.Peek() != '(')
                    {
                        postfix.Enqueue(stack.Pop().ToString());
                    }

                    stack.Pop();

                    if (x != exp.Length - 1 && exp[x + 1] == '(')
                    {
                        while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence('*', stack.Peek()) == true)
                        {
                            postfix.Enqueue(stack.Pop().ToString());
                        }
                        stack.Push('*');
                    }
                }
            }

            while (!stack.IsEmpty())
            {
                postfix.Enqueue(stack.Pop().ToString());
            }

            return(postfix);
        }