Ejemplo n.º 1
0
        public int Pop()
        {
            int element = _array[_array.Length - 1];

            _array = HelpersForSet.CompressArray(_array);

            return(element);
        }
Ejemplo n.º 2
0
        public void Push(int element)
        {
            Stack lastStack     = _arrayOfStacks[_arrayOfStacks.Length - 1];
            int   lengthOfStack = lastStack.GetLength();

            if (lengthOfStack < 5)
            {
                lastStack.Push(element);
            }

            else
            {
                Stack newStack = new Stack();
                newStack.Push(element);

                _arrayOfStacks = HelpersForSet.ExpandSetOfStacks(_arrayOfStacks, newStack);
            }
        }
Ejemplo n.º 3
0
        public int Pop()
        {
            int   element       = 0;
            Stack lastStack     = HelpersForSet.GetLastStack(_arrayOfStacks);
            int   lengthOfStack = lastStack.GetLength();

            if (lengthOfStack > 0)
            {
                element = lastStack.Pop();
            }
            else
            {
                _arrayOfStacks = HelpersForSet.CompresSetOfStacks(_arrayOfStacks);
                lastStack      = _arrayOfStacks[_arrayOfStacks.Length - 1];
                element        = lastStack.Pop();
            }

            return(element);
        }
Ejemplo n.º 4
0
        public int Peek()
        {
            int   element   = 0;
            Stack lastStack = HelpersForSet.GetLastStack(_arrayOfStacks);

            int legthOfStack = lastStack.GetLength();

            if (legthOfStack > 0)
            {
                element = lastStack.Peek();
            }

            else
            {
                Console.WriteLine("InvalidOperationException");
            }

            return(element);
        }
Ejemplo n.º 5
0
 public void PushElement(int element, SetOfStacks setOfStacks)
 {
     element = HelpersForSet.ReadInt(" ");
     setOfStacks.Push(element);
 }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.Write("1- push element; ");
            Console.Write("2 - pop element; ");
            Console.Write("3 - popAt element by index; ");
            Console.Write("4 - peek; ");
            Console.Write("0 - break; ");
            int element = 0;
            int number  = 0;

            SetOfStacks setOfStacks = new SetOfStacks();

            setOfStacks = new SetOfStacks();

            do
            {
                Console.WriteLine();

                number = HelpersForSet.ReadInt("what you choose? - ");

                if (number == 1)
                {
                    while (true)
                    {
                        element = HelpersForSet.ReadInt(" ");


                        if (element == 0)
                        {
                            break;
                        }
                        else
                        {
                            int lengthOfLastStack = setOfStacks.GetLengthLastStack(setOfStacks);

                            if (lengthOfLastStack == 4)
                            {
                                Console.WriteLine();
                                setOfStacks.Push(element);
                            }

                            else
                            {
                                setOfStacks.Push(element);
                            }
                        }
                    }
                }
                else if (number == 2)
                {
                    element = setOfStacks.Pop();
                    Console.WriteLine($"pop = {element}");
                }
                else if (number == 3)
                {
                    int index = HelpersForSet.ReadInt("index = ");
                    element = setOfStacks.PopAt(index);
                    Console.WriteLine($"pop by stack index {index} = {element}");
                }
                else if (number == 4)
                {
                    element = setOfStacks.Peek();
                    Console.WriteLine($"peek = {element}");
                }
                else
                {
                    break;
                }
            }while (number != 0);

            Console.ReadKey();
        }
Ejemplo n.º 7
0
 public void Push(int newElement)
 {
     _array = HelpersForSet.ExpandArray(_array, newElement);
 }