Пример #1
0
        static void Main()
        {
            ArrayStack <int> Stack = new ArrayStack <int>();

            Console.WriteLine(Stack.Count);


            Stack.Push(5);

            Console.WriteLine(Stack.Count);

            Console.WriteLine(Stack.Peek());

            Console.WriteLine(Stack.Pop());

            Console.WriteLine(Stack.Count);


            Stack.Push(7);

            Console.WriteLine(Stack.Peek());
            Console.WriteLine(Stack.Count);

            Stack.Pop();
            Stack.Pop();
        }
Пример #2
0
 public void IsEmptyTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(123);
     myList.Pop();
     Assert.AreEqual(true, myList.IsEmpty());
     Assert.AreEqual(true, myList.IsEmpty());
 }
Пример #3
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");
            }
        }
Пример #4
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();
 }
Пример #5
0
 public void Pop_LowerBorderTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(int.MinValue);
     Assert.AreEqual(int.MinValue, myList.Pop());
 }
Пример #6
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());
 }
Пример #7
0
 public void PopTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Push(123456);
     Assert.AreEqual(123456, myList.Pop());
 }
Пример #8
0
 public void PopExceptionTest()
 {
     ArrayStack myList = new ArrayStack();
     myList.Pop();
 }
Пример #9
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());
 }