Пример #1
0
        public T PopAt(int index)
        {
            if (index == 0)
            {
                return(Pop());
            }
            SetOfStacks <T> cursor = _previous;

            while (index-- > 0)
            {
                if (cursor._previous == null)
                {
                    throw new ArgumentOutOfRangeException();
                }
                cursor = cursor._previous;
            }
            return(cursor.Pop());
        }
Пример #2
0
        public T Pop()
        {
            while (_count == 0)
            {
                if (_previous == null)
                {
                    throw new ArgumentOutOfRangeException();
                }
                _count    = _previous._count;
                _head     = _previous._head;
                _previous = _previous._previous;
            }

            var value = _head.Value;

            _head = _head.Next;
            _count--;
            return(value);
        }
Пример #3
0
        public void Push(T value)
        {
            if (_count >= _capacity)
            {
                _previous = new SetOfStacks <T>(this);
                _count    = 0;
                _head     = null;
            }
            var node = new Node(value);

            if (_head == null)
            {
                _head = node;
            }
            else
            {
                node.Next = _head;
                _head     = node;
            }
            _count++;
        }