예제 #1
0
 public void IsEmptyTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(123);
     myList.Pop();
     Assert.AreEqual(true, myList.IsEmpty());
     Assert.AreEqual(true, myList.IsEmpty());
 }
예제 #2
0
 public void DontPushTest()
 {
     ArrayStack myList = new ArrayStack();
     for (int i = 0; i < 100; i++)
     {
         myList.Push(i);
     }
     myList.Push(100);
 }
예제 #3
0
 public void LastTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(0);
     myList.Push(1);
     Assert.AreEqual(1, myList.Pop());
     myList.Push(2);
     myList.Push(3);
     myList.Push(2);
     Assert.AreEqual(2, myList.Pop());
     Assert.AreEqual(3, myList.Pop());
     Assert.AreEqual(2, myList.Pop());
     Assert.AreEqual(0, myList.Pop());
     Assert.AreEqual(true, myList.IsEmpty());
     myList.Pop();
 }
예제 #4
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            ArrayStack myStack = new ArrayStack();
            StackCalculator myCalculator = new StackCalculator(myStack);
            myCalculator.Push(3);
            myCalculator.Push(2);
            myCalculator.Push(1);
            myCalculator.Add();
            myCalculator.Multiply();
            Console.WriteLine(myCalculator.Result());

            ListStack myStack2 = new ListStack();
            StackCalculator myCalculator2 = new StackCalculator(myStack2);
            myCalculator2.Push(1);
            myCalculator2.Push(2);
            myCalculator2.Push(3);
            myCalculator2.Add();
            myCalculator2.Multiply();
            Console.WriteLine(myCalculator2.Result());
        }
예제 #5
0
        public static void PrintNextGreaterElementInRightSideForAll(int[] arr)
        {
            if (arr.Length == 0)
            {
                return;
            }

            MyStack <int> myStack = new ArrayStack <int>(arr.Length);

            myStack.Push(arr[0]);
            for (int i = 1; i < arr.Length; i++)
            {
                int currentItem = arr[i];
                if (!myStack.IsEmpty())
                {
                    int peekedItem = myStack.Peek();
                    while (currentItem > peekedItem) //we found greater for some item
                    {
                        Console.WriteLine("For item " + peekedItem + ", next greater is: " + currentItem);
                        myStack.Pop(); //Remove that processed item

                        if (!myStack.IsEmpty())
                        {
                            peekedItem = myStack.Peek(); //there can be many small than current item in stack
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                //Add current item to stack
                myStack.Push(currentItem);
            }

            //Once we are done with this, all items in stack doesn't have greater on right.
            while (!myStack.IsEmpty())
            {
                Console.WriteLine("For item " + myStack.Pop() + ", next greater is: " + "NOT-FOUND");
            }
        }
        public void Run()
        {
            ArrayStack <int> arrayStack = new ArrayStack <int>(10);

            arrayStack.Push(2);
            arrayStack.Push(5);
            arrayStack.Push(1);
            arrayStack.Push(6);

            foreach (var item in arrayStack)
            {
                Console.WriteLine("Item:" + item);
            }

            Console.WriteLine("---------");
            SllStack <int> sllStack = new SllStack <int>(); //no need of any capacity

            sllStack.Push(2);
            sllStack.Push(5);
            sllStack.Push(1);
            sllStack.Push(6);

            foreach (var item in sllStack)
            {
                Console.WriteLine("Item:" + item);
            }

            Console.WriteLine("---------");
            DllStack <int> dllStack = new DllStack <int>(); //no need of any capacity

            dllStack.Push(1);
            Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData());
            dllStack.Push(2);
            Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData());
            dllStack.Push(3);
            Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData());
            dllStack.Push(4);
            Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData());

            foreach (var item in dllStack)
            {
                Console.WriteLine("Item:" + item);
            }
            Console.WriteLine("---------");

            SpecialStack <int> specialStack = new SpecialStack <int>(); //no need of any capacity

            specialStack.Push(2);
            specialStack.Push(5);
            specialStack.Push(1);
            specialStack.Push(6);

            while (!specialStack.IsEmpty())
            {
                Console.WriteLine("Item:" + specialStack.Peek() + ", min:" + specialStack.GetMin() + ", max:" + specialStack.GetMax());
                specialStack.Pop();
            }
            Console.WriteLine("---------");

            Console.WriteLine("Balanced: " + StackAlgos.AreParenthesisBalanced("[(1*2)+{5+6}"));
            Console.WriteLine("---------");

            StackAlgos.ReverseStackUsingStackOperationsOnly(arrayStack);
            Console.WriteLine("After reverse");
            foreach (var item in arrayStack)
            {
                Console.WriteLine("Item:" + item);
            }
            Console.WriteLine("---------");

            StackAlgos.PrintNextGreaterElementInRightSideForAll(new int[] { 10, 4, 5, 90, 120, 80 });
            Console.WriteLine("---------");

            StackAlgos.PrintStockSpan(new int[] { 10, 4, 5, 90, 120, 80 });

            Console.WriteLine("---------");

            StackAlgos.MaxAreaRectangleFromHistogram(new int[] { 6, 2, 5, 4, 5, 1, 6 });
            Console.WriteLine("---------");

            int count = StackAlgos.MinNumberOfReversalNeededToMakeExpressionValid("}{{}}{{{");

            Console.WriteLine("Number of reversal nedded:" + count);
            Console.WriteLine("---------");

            int disk = 4;

            StackAlgos.TowerOfHanoi(disk);
            Console.WriteLine("---------");

            Console.ReadKey();
        }
예제 #7
0
 public void Pop_LowerBorderTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(int.MinValue);
     Assert.AreEqual(int.MinValue, myList.Pop());
 }
예제 #8
0
 public void PopTest2()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(123);
     myList.Push(321);
     myList.Push(132);
     Assert.AreEqual(132, myList.Pop());
     Assert.AreEqual(321, myList.Pop());
     Assert.AreEqual(123, myList.Pop());
 }
예제 #9
0
 public void PopTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(123456);
     Assert.AreEqual(123456, myList.Pop());
 }
예제 #10
0
 public void PopExceptionTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Pop();
 }
예제 #11
0
 public void InitialStateTest()
 {
     ArrayStack myList = new ArrayStack();
 }
예제 #12
0
 public void TopTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(12);
     Assert.AreEqual(12, myList.Top());
 }
예제 #13
0
 public void Push_BordersTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(int.MinValue);
     myList.Push(int.MaxValue);
 }
예제 #14
0
 public void PushTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(8);
 }
예제 #15
0
 public void PushAndPopTest()
 {
     ArrayStack myList = new ArrayStack();
     for (int i = 0; i < 100; i++)
     {
         myList.Push(i);
     }
     for (int i = 99; i > -1; i--)
     {
         Assert.AreEqual(i, myList.Pop());
     }
     Assert.AreEqual(true, myList.IsEmpty());
 }